Hi !
I am long time user of hibernate, and now I tried to "sell" NHibernate to my current boss, because we starting on new project. Problem is that I can't get it to work...
Problem is with ID generator(s). I have tried several, I read all posts on internet I could find but nothing helps....
I am testing on two different databases, problem is always the same.
Here is hbm.xml (part)
<class name="ArticleH" table="Articles" lazy="false"> <id name="Id" column="ArticleID" type="Int32"> <generator class="native" /> </id>
<property name="Name" column="ArticleName" type="string" length="50" /> <property name="InsertDate" type="DateTime" />
</class>
---
Code is quite simple:
public object AddHibernate(Object obj) {
log.Info("addHibernate::" + obj.ToString());
ITransaction tx = null; try { ISession sess = GetNewSession(); tx = sess.BeginTransaction();
object val = sess.Save(obj); tx.Commit();
return val; } catch (Exception ex) { if (tx != null) tx.Rollback();
log.Error("Exception on addHibernate: " + ex, ex); return -1; }
}
This is method for adding. Problem comes on Commit... So I tried several generators (native, identity and some others). At first I thought that it's database problem, so I made also PostgreSQL tables on PgSQL database (I started work with MS SQL 2005)
Error is same on both databases.
5687 [10] ERROR HibernateUtil (null) - Exception on addHibernate: NHibernate.HibernateException: identifier of an instance of Comtron.TicDemo.Datalayer.HObjects.ArticleH was altered from 5 to 5 at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEntityEventListener.cs:line 116 at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISession Implementor session) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\Def aultFlushEntityEventListener.cs:line 81 at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(Flu shEntityEvent event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\Def aultFlushEntityEventListener.cs:line 36 at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(Flush Event event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlu shingEventListener.cs:line 161 at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToEx ecutions(FlushEvent event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Defau lt\AbstractFlushingEventListener.cs:line 60 at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent even t) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventList ener.cs:line 18 at NHibernate.Impl.SessionImpl.Flush() in c:\CSharp\NH\nhibernate\src\NHibern ate\Impl\SessionImpl.cs:line 1477 at NHibernate.Transaction.AdoTransaction.Commit() in c:\CSharp\NH\nhibernate\ src\NHibernate\Transaction\AdoTransaction.cs:line 187 at HibernateUtil.AddHibernate(Object obj) in D:\workNet\AtechTools\trunk\dot_ net\AtechToolsNet\Db\NHibernate\HibernateUtil.cs:line 226 NHibernate.HibernateException: identifier of an instance of Comtron.TicDemo.Data layer.HObjects.ArticleH was altered from 5 to 5 at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object ob j, IEntityPersister persister, Object id, EntityMode entityMode) in c:\CSharp\NH \nhibernate\src\NHibernate\Event\Default\DefaultFlushEntityEventListener.cs:line 116 at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISession Implementor session) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\Def aultFlushEntityEventListener.cs:line 81 at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(Flu shEntityEvent event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\Def aultFlushEntityEventListener.cs:line 36 at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(Flush Event event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlu shingEventListener.cs:line 161 at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToEx ecutions(FlushEvent event) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Defau lt\AbstractFlushingEventListener.cs:line 60 at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent even t) in c:\CSharp\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventList ener.cs:line 18 at NHibernate.Impl.SessionImpl.Flush() in c:\CSharp\NH\nhibernate\src\NHibern ate\Impl\SessionImpl.cs:line 1477 at NHibernate.Transaction.AdoTransaction.Commit() in c:\CSharp\NH\nhibernate\ src\NHibernate\Transaction\AdoTransaction.cs:line 187 at HibernateUtil.AddHibernate(Object obj) in D:\workNet\AtechTools\trunk\dot_ net\AtechToolsNet\Db\NHibernate\HibernateUtil.cs:line 226
If anyone has any idea what is happening I would be thankful for any help.... And to answer some questions before hand...
I use new Session each time (I tried with using one Session for lifetime of application, as I do with java, but since one article said to create session for each operation I tried that too).
And yes, I get ID from database back, so I think that problem is on nhibernate side.
Andy
|