-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Session in a threaded environment
PostPosted: Thu Jul 24, 2008 5:04 am 
Newbie

Joined: Wed Jul 23, 2008 10:48 am
Posts: 5
Hi all,

I have a problem with a session. I have a service class, which starts a new thread (triggering another service). Both services have the same Hibernate session injected via hivemind.
But on the commit I got a NonUniqueObjectException.

Hibernate version:
3.2.6.GA.

Full stack trace of any exception that occurs:

Code:
2008-07-24 10:30:22,204 ERROR org.hibernate.impl.SessionImpl:399 - exception in interceptor beforeTransactionCompletion()
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [core.entity.model.TranslatableText#2]
        at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
        at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
        at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
        at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
        at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
        at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
        at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
        at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
        at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
        at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at com.javaforge.honeycomb.hibernate.audit.AuditInterceptor.beforeTransactionCompletion(AuditInterceptor.java:211)
        at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:396)
        at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:206)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:111)
        at com.javaforge.honeycomb.service.AbstractPersistenceService.commit(AbstractPersistenceService.java:73)
        at $PersistenceSvc_11b54302d06.commit($PersistenceSvc_11b54302d06.java)
        at $PersistenceSvc_11b54302cf7.commit($PersistenceSvc_11b54302cf7.java)
        at test.process.integrationstest.IntegrationTests.withoutCorporateDocument(IntegrationTests.java:340)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:478)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
        at org.testng.TestRunner.runWorkers(TestRunner.java:712)
        at org.testng.TestRunner.privateRun(TestRunner.java:582)
        at org.testng.TestRunner.run(TestRunner.java:477)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
        at org.testng.SuiteRunner.run(SuiteRunner.java:198)
        at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:821)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:788)
        at org.testng.TestNG.run(TestNG.java:708)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:74)
        at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:92)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)


Name and version of the database you are using:
HSQLDB 1.8.0

Have anyone have a simular problem? Or any hint, what the problem could be?

Thank you for your answers,
Marco
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:53 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Make sure you have the hashCode and equals method properly overridden in the entity classes. Perhaps the
Hibernate session thinks two instances that are really the same, are actually different?

Just a shot in the dark. :(

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 4:34 am 
Newbie

Joined: Wed Jul 23, 2008 10:48 am
Posts: 5
Hi Cameron,

thank you for your response.
I've tried your suggestion and implement the equals and hashCode methods, but exception still occures. So your shot miss the target :-(

Any further suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:33 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Hibernate's reference document says that you must override equals and hashcode if you want to put instances in sets or you want to use reattachment of detached objects. Well i agree with first point but as far as second point is concerned i don't agree because hibernate does not match two objects by calling equals or hashcode i have tried it out. It i think does through id of objects.

Back to your problem, this problem occurs when you going to save an object which already contains different object but with same id. e.g. you have created two Person objects with new and set same parameters to both objects including same id. Now when you will save it in a same session, you will get exception which you are getting or there is another case when you face this problem. e.g. you have an object say A which contains object B and C, Now Object B also contains object C. Now if you have received it from client side or you have detached it, then when you will try to save A, you will get error. I faced this type of error when working in web services.

_________________
amer sohail


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.