NHibernate会保存,但不会加载实体
我在NHibernate 2.1.2.4000 GA和最新版本的FluentNHibernate中收到了一个非常奇怪的错误。 我可以保存一个实体,但不能在调用Flush()和Clear()后加载它。
这是我的实体:
public class Application
{
public int Id { get; set; }
public string Name { get; set; }
public string KeyName { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public override bool Equals(object obj)
{
if (null != obj && obj is Application)
{
return ((Application)obj).Id == this.Id;
}
else
{
return base.Equals(obj);
}
}
}
我的配置地图:
public class ApplicationMap : ClassMap
{
public ApplicationMap()
{
Table("[Application]");
Not.LazyLoad();
Id(x => x.Id, "ApplicationId")
.GeneratedBy.Identity();
Map(x => x.Name, "ApplicationName")
.Nullable()
.Length(50);
Map(x => x.Description, "ApplicationDescription")
.Nullable()
.Length(200);
Map(x => x.KeyName, "ApplicationKeyName")
.Nullable()
.Length(50);
Map(x => x.Url, "ApplicationLink")
.Nullable()
.Length(50);
}
}
我如何创建我的ISession:
var _sessionFactory = Fluently.Configure() .Database( SQLiteConfiguration.Standard.InMemory() .ProxyFactoryFactory(typeof(ProxyFactoryFactory))) .Mappings( m => m.FluentMappings.AddFromAssemblyOf()) .ExposeConfiguration(cfg => Config = cfg) .BuildSessionFactory(); var _session = _sessionFactory.OpenSession();
这是不能工作的代码:
Application myApp = new Application()
{
Id = 1,
Description = "MyApp",
KeyName = "MyApp",
Name = "My App",
Url = "http://www.myapp.com"
};
_session.Save(myApp);
var idMyApp = myApp.Id;
_session.Flush();
_session.Clear();
_session = NHibernateHelper.CreateSession();
var a = _session.Load(idMyApp);
我尝试从数据库加载对象返回的异常是:
Test method HNI.Portal.Test.MappingTests.RoleMap.CanCorrectMapRole threw exception: NHibernate.ObjectNotFoundException: No row with the given identifier exists[HNI.Portal.Core.Entities.Application#1].
和StackTrace:
NHibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(String entityName, Object id) NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) NHibernate.Impl.SessionImpl.Load(String entityName, Object id) NHibernate.Impl.SessionImpl.Load(Type entityClass, Object id) NHibernate.Impl.SessionImpl.Load[T](Object id)
我不确定是否应该调用Flush()和Clear()(仍在研究它),但是在使用PersistenceSpecification编写测试时遇到此错误。 这是PersistenceSpecification如何在CheckList()声明中验证列表是否正确保存,这不适合我。 我进入代码并得到这个repro。
应用程序行已正确插入数据库,但不能再次加载。 它同时发生在SqlServer和Sqlite上。
希望你们能帮助我。
非常感谢!
您正在设置该ID,但它是一个身份。 你不应该设置它,SQL为你做。
而不是创建一个新的会话使用相同的,并使用获取而不是负载检索。
加载和获取之间的区别:http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx
我认为你太早从我的myApp获取Id了。 在查找Id之前尝试执行_session.Flush() 。 由于您使用的是身份生成器,因此NHibernate会将生成的Id复制到您的对象,但是直到会话刷新(通常在事务提交时)才会发生这种情况。
你使用两种不同的方式来创建会话?
NHibernateHelper.CreateSession();
和
_sessionFactory.OpenSession();
也许你应该尝试_sessionFactory.OpenSession(); 获取一个会话进行检索。
链接地址: http://www.djcxy.com/p/71053.html