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


I´m receiving a very strange error with NHibernate 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;
   return base.Equals(obj);

My configuration Map:

public class ApplicationMap : ClassMap
 public ApplicationMap()


  Id(x => x.Id, "ApplicationId")
  Map(x => x.Name, "ApplicationName")
  Map(x => x.Description, "ApplicationDescription")
  Map(x => x.KeyName, "ApplicationKeyName")
  Map(x => x.Url, "ApplicationLink")

How I create my ISession:

var _sessionFactory = Fluently.Configure()
  m => m.FluentMappings.AddFromAssemblyOf())
 .ExposeConfiguration(cfg => Config = cfg)

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"

var idMyApp = myApp.Id;
_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