R – NHibernate will save, but won´t load an entity

fluent-nhibernateloadnhibernatesavetesting

I´m receiving a very strange error with NHibernate 2.1.2.4000 GA and latest version of FluentNHibernate. I can save an entity but can´t load it back after a call to Flush() and Clear().

This is my entity:

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

How I create my 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();

And this is the code that won´t work:

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

The Exception I get at the moment I try to Load the object back from database is:

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].

and the 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)

I´m not sure if I should call Flush() and Clear() (still doing research on it), but I´m getting this error while writing tests with PersistenceSpecification. This is how PersistenceSpecification does on CheckList() assert to verify if a list is being correctly saved, which is not for me. I stepped into the code and got to this repro.

The application row is correctly inserted in the database but it won´t load again. It happens both with SqlServer and Sqlite.

Hope you guys can help me.

Thanks a lot!

Best Solution

You are setting the id but it's an Identity. You should not set it, SQL does that for you.

Instead of creating a new session use the same one, and retrieve using Get instead of Load.

On the difference between Load and Get: http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx

Related Question