CarbonMan
CarbonMan

Reputation: 4490

nHibernate: Unable to locate persister

Running VS2003, nHB 3.0.0.3001, trying to run a unit test through Gallio 3.2. The code is from the first session of "Summer of nHibernate". I am getting the following error when attempting to run the test.

NHibernate.HibernateException: Unable to locate persister: DataTransfer.Customer
TargetSite: Void OnLoad(NHibernate.Event.LoadEvent, NHibernate.Event.LoadType)
HelpLink: null
at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
at NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
at NHibernate.Impl.SessionImpl.Get[T](Object id)
at DataAccessLayer.NhibernateDataProvider.GetCustomerById(Int32 customerId) 

My very uneducated guess is that the Customer class cannot be seen. But both the unit test and the data access layers have a reference to the DataTransfer assembly that holds the Customer class. The Customer class is public and all the properties are public virtual.

The nHB config file is in the directory, and the Customer mapping file is an embedded resource. Seems to fail on the line:

return session.Get<Customer>(customerId);

Upvotes: 9

Views: 27438

Answers (3)

Willy David Jr
Willy David Jr

Reputation: 9151

On your hibernate.cfg.xml file, add this code:

 <mapping assembly="(name of your Assembly Project)" />

Basically, on your code its:

 <mapping assembly="DataTransfer" />

Upvotes: 0

EladTal
EladTal

Reputation: 2846

I'm using ActiveRecord for entity mapping, got the same error when tried to use ISession.Get, instead of using configuration for retrieving session, like this:

                     ISession session = new NHibernate.Cfg.Configuration()
                    .Configure()
                    .AddAssembly(typeof(Account).Assembly)
                    .SetInterceptor(new SqlStatementInterceptor())
                    .BuildSessionFactory().OpenSession();

I changed it to work through ActiveRecordMediator:

                    ISession session = Castle.ActiveRecord.ActiveRecordMediator
                    .GetSessionFactoryHolder().
                    CreateSession(typeof(Castle.ActiveRecord.ActiveRecordBase));

now its working great..

Upvotes: 0

Bob Palmer
Bob Palmer

Reputation: 4762

  1. Make sure your mapping file is named *.hbm.xml

  2. Make sure it is set to an embedded resource.

  3. If these fail, debug and when it fails, catch the exception and look at the InnerException - NHibernate has fantastic error messaging hidden in there.

Hope those thigs help out - otherwise we can look into some other options (but 1 and 2 solve 90% of persister problems)

Upvotes: 29

Related Questions