nHibernate: Unable to locate persister - nhibernate

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

Make sure your mapping file is named *.hbm.xml
Make sure it is set to an embedded resource.
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)

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

On your hibernate.cfg.xml file, add this code:
<mapping assembly="(name of your Assembly Project)" />
Basically, on your code its:
<mapping assembly="DataTransfer" />

Related

Entity Framework: .Add & .SaveChanges() is not adding entity to database

When I am adding an entry
var objectEntity = new ObjectStaging();
objectEntity .CreatedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .ModifiedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .Set(ObjectInfo);
database.ObjectStaging.Add(objectEntity);
database.SaveChanges();
For some reason, my objectEntity is not being added to my database given the following code. When using the debugger, it moves through database.ObjectStaging.Add(objectEntity); but does not continue after calling SaveChanges(). Is there a way to know why it is failing to add the entry to the database?
Exception Thrown:
Exception thrown:
'System.Data.Entity.Validation.DbEntityValidationException' in
VCC.BrokerPortal.DAO.dll Validation failed for one or more entities.
See 'EntityValidationErrors' property for more details.
Is there a way to know what specifically did not validate?
Also, I tested the state of the entity using the following lines of code,
database.ObjectStaging.Add(objectEntity);
var state= brokerPortalDB.Entry(objectEntity).State;
state currently shows the value of 'added'.
Thank you!
I ended up looking into the exception thrown and it appears it was related to one of the properties not having proper validation. In this instance I had a [MaxLength] attribute assigned to a field and I was trying to add a value longer than that length allowed.

showing exception on declaring new data member in class file using petapoco data access framework

I am using petapoco dataaccess framework, after declaring one new parameter in class file according to database table i am getting the exception below mentioned, in windows service at starting the service.
Service cannot be started. System.MissingMethodException: Method not found: 'System.String projectname.Models.EmailTemplatePOCO.get_Subject()'.
at EmailService.EmailService.StartEmailService()
at EmailService.EmailService.ScheduledService() in d:\Project\Projectname\trunk\projectname\EmailService\EmailService.cs:line 85
at EmailService.EmailService.OnStart(String[] args) in d:\Project\projectname\trunk\projectname\EmailService\EmailService.cs:line 66
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
Does your poco use properties? It will need to eg;
public string Subject {get; set;}
Not sure if you're getting a petapoco error or not

How to save and then update same class instance during one request with NHibernate?

I'm relatively new to NHibernate and I've got a question about it.
I use this code snippet in my MVC project in Controller's method:
MyClass entity = new MyClass
{
Foo = "bar"
};
_myRepository.Save(entity);
....
entity.Foo = "bar2";
_myRepository.Save(entity);
The first time entity saved in database succesfully. But the second time not a single request doesnt go to database. My method save in repository just does:
public void Save(T entity)
{
_session.SaveOrUpdate(entity);
}
What should I do to be able to save and then update this entity during one request? If I add _session.Flush(); after saving entity to database it works, but I'm not sure, if it's the right thing to do.
Thanks
This is the expected behavior.
Changes are only saved on Flush
Flush may be called explicitly or implicitly (see 9.6. Flush)
When using an identity generator (not recommended), inserts are sent immediately, because that's the only way to return the ID.
you should be using transactions.
a couple of good sources: here and here.
also, summer of nHibernate is how I first started with nHibernate. it's a very good resource for learning the basics.

NHibernate: No persister for NHibernate.Criterion.SqlFunctionProjection

I am using NHibernate version 2.0.0.4000.
In one of my queries I wanted to make use of the sql function dateadd to add a number of days. This wasn't registered so I created my own dialect and registered the function as follows:
RegisterFunction("adddays",
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"dateadd(dd, ?1, ?2)"));
The registration gets hit and seems to work fine. I use the function in a DetachedCriteria query as follows:
...
Restrictions.LtProperty("DateColumn1"
Projections.SqlFunction("adddays", NHibernateUtil.DateTime,
Projections.Constant(days),
Projections.Property("DateColumn2"))
...
The criteria is returned from a method and passed of to another query. Upon the execution of the final query I get the following exception:
NHibernate.MappingException was caught
Message="No persister for: NHibernate.Criterion.SqlFunctionProjection"
Source="NHibernate"
StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName,
Boolean throwIfNotFound)
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
...
None of the blog posts that I have seen mention this problem. Can anybody help?
Cheers in advance.
Nige.
Solved it.
The problem was caused by my usage elsewhere of Restrictions.Eq rather than Restrictions.EqProperty. The former does not have an overload for (IProjection,IProjection) and so was treating the second projection as an object and passing it to a persister.
Thanks to anyone who investigated this.
Nigel.
For No Persister exception in nhibernet solution is
just give a right click on the respective hbm file ->Properties->change the type Build Action-> to Embedded Resource
with the above step application will working fine

Private property mapping with fluent nhibernate

I am getting exception mapping a private property.This is the situation:
I have this in Entity.cs:
privat int m_Inactive;
and in EntityMap.cs I have :
Map(x => Reveal.Property<Entity>("m_Inactive")).ColumnName.("INACTIVE");
But I get this error:
System.Reflection.TargetInvocationException: Exception has been thrown
by
the target of an invocation. ---> System.ArgumentException: Not a member access
What could be the reason?
Thanks.
If you follow the examples on the wiki you'll see that you're supposed to use Map(Reveal.Member<YourEntity>("m_Inactive")).
Looks like in the latest version you're supposed to use Reveal.Member since Reveal.Property is obsolete:
Map(Reveal.Member<YourEntity>("m_Inactive"))
Oh, and sort of a "duh" but you'll need to make sure you include FluentNHibernate:
using FluentNHibernate;
And another "duh" but this will work with protected members as well as private.