-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with insert
PostPosted: Tue Aug 18, 2009 12:13 pm 
Newbie

Joined: Thu Nov 25, 2004 5:56 am
Posts: 7
Location: Slovenia, Europe
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


Top
 Profile  
 
 Post subject: Re: Problem with insert
PostPosted: Wed Aug 19, 2009 4:53 am 
Newbie

Joined: Thu Nov 25, 2004 5:56 am
Posts: 7
Location: Slovenia, Europe
Hi !

Me again... I found sollution. It seems that session is per default in autocommit mode, which doesn't work OK with Transactions (double commit). So we can do following:

A.) Don't use transaction:

ISession sess = GetNewSession();
object val = sess.Save(obj);

or

B.) Disable autocommit

ISession sess = GetNewSession();
sess.FlushMode = FlushMode.Never;

tx = sess.BeginTransaction();

object val = sess.Save(obj);
tx.Commit();


I am sorry that I bothered all of you, but I never had this problems in java (not in all of 5 years, since I am using it)

Andy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.