NHibernate will save but not load an object

I get a very strange error with NHibernate 2.1.2.4000 GA and the latest version of FluentNHibernate. I can save the object, but I cannot load it after calling Flush () and Clear ().

This is my essence:

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);
  }
 }
}

My configuration map:

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(), , , . .

​​ , . SqlServer, Sqlite.

, , , .

!

+5
4

, Id myApp. _session.Flush(), Id. , NHibernate , , ( ).

+1

?

NHibernateHelper.CreateSession();

_sessionFactory.OpenSession();

, _sessionFactory.OpenSession(); .

0

you don’t need to crash manually, as the infrastructure will notice that you are searching in the entity on which you are still waiting for changes, these changes will be cleared before receiving, in this I think idMyApp will be 1 instead of the correctly generated id from the database .. .. causing your code to crash ...

0
source

All Articles