Announcement Announcement Module
Collapse
No announcement yet.
NonUniqueObjectException while saveorupdate within a transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NonUniqueObjectException while saveorupdate within a transaction

    I am working on an application using Struts/Spring/Hibernate. I have struts calling services using spring's declarative transactions which then call the DAO which extends HibernateDAOSupport.

    There is no lazy loading for any domain objects or collections. I have an object that has one to many relationships with it's children. The problem I am having is that when I save this object the first time it works fine but when I try to do an update, it fails with a NonUniqueObjectException.

    I am calling getHibernateTemplate().saveOrUpdate(object). To further describe what excatly happens, when Object is saved the first time, we store it in the HttpSession. When user attempts to update this, I get it out of the HttpSession and map the modified attribute and call the saveOrUpdate. I donot have a unsave-value defined in my mapping. I can see in the logs that a new hibernate Session is opened before it tried to make the update. But it still gives the message " different object with the same identifier value was already associated with the session". I am copying the logs for reference:

    -------------------------------------------------------------------

    2005-11-24 17:05:27,072 ERROR com.roche.dss.affiliatedatacapture.web.action.LogE xceptionAction [ExecuteThread: '13' for queue: 'weblogic.kernel.Default'] Caught org.springframework.orm.hibernate3.HibernateSystem Exception: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]
    org.springframework.orm.hibernate3.HibernateSystem Exception: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]
    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]
    at org.hibernate.engine.PersistenceContext.checkUniqu eness(PersistenceContext.java:586)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.performUpdate(DefaultSaveOrUpdateEventListe ner.java:254)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsDetached(DefaultSaveOrUpdateEventLi stener.java:214)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.performSaveOrUpdate(DefaultSaveOrUpdateEven tListener.java:91)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList ener.java:69)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(Sessio nImpl.java:468)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(Sessio nImpl.java:463)
    at org.springframework.orm.hibernate3.HibernateTempla te$18.doInHibernate(HibernateTemplate.java:673)
    at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:358)
    at org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:670)
    at com.roche.dss.extreportdomainmodel.dao.hibernate.R eportDAOHibernate.saveOrUpdateReport(ReportDAOHibe rnate.java:46)
    at com.roche.dss.affiliatedatacapture.service.impl.Re portServiceImpl.save(ReportServiceImpl.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:292)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:155)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :122)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:96)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :144)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:174)
    at $Proxy14.save(Unknown Source)
    at com.roche.dss.affiliatedatacapture.web.action.Save ReportAction.executeValidToken(SaveReportAction.ja va:66)
    at com.roche.dss.affiliatedatacapture.web.action.Sync hroAction.execute(SynchroAction.java:40)
    at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
    at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
    at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
    at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:415)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:760)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at weblogic.servlet.internal.ServletStubImpl$ServletI nvocationAction.run(ServletStubImpl.java:996)
    at weblogic.servlet.internal.ServletStubImpl.invokeSe rvlet(ServletStubImpl.java:419)
    at weblogic.servlet.internal.TailFilter.doFilter(Tail Filter.java:28)
    at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:27)
    at com.roche.dss.security.navigation.LoginFilter.doFi lter(LoginFilter.java:192)
    at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:27)
    at weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.run(WebAppServletContext.java :6458)
    at weblogic.security.acl.internal.AuthenticatedSubjec t.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(Se curityManager.java:118)
    at weblogic.servlet.internal.WebAppServletContext.inv okeServlet(WebAppServletContext.java:3661)
    at weblogic.servlet.internal.ServletRequestImpl.execu te(ServletRequestImpl.java:2630)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThrea d.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.ja va:178)



    -------------------------------------------------------------------------
    org.springframework.transaction.interceptor.Transa ctionInterceptor [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Getting transaction for com.roche.dss.affiliatedatacapture.service.ReportS ervice.save
    2005-11-24 16:41:42,500 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Using transaction object [org.springframework.orm.hibernate3.HibernateTransa ctionManager$HibernateTransactionObject@1c8ce14]
    2005-11-24 16:41:42,500 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Creating new transaction with name [com.roche.dss.affiliatedatacapture.service.ReportS ervice.save]
    2005-11-24 16:41:42,500 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Opened new Session [org.hibernate.impl.SessionImpl@51b326] for Hibernate transaction
    2005-11-24 16:41:42,516 DEBUG com.roche.dss.affiliatedatacapture.service.impl.Re portServiceImpl [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] begin save(report)
    2005-11-24 16:41:42,563 DEBUG 2005-11-24 16:42:15,032 DEBUG org.springframework.orm.hibernate3.HibernateTempla te [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Found thread-bound Session for HibernateTemplate
    2005-11-24 16:42:15,063 DEBUG org.springframework.orm.hibernate3.HibernateTempla te [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Not closing pre-bound Hibernate Session after HibernateTemplate
    2005-11-24 16:42:18,453 DEBUG org.springframework.transaction.interceptor.RuleBa sedTransactionAttribute [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Applying rules to determine whether transaction should rollback on org.springframework.orm.hibernate3.HibernateSystem Exception: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]

    2005-11-24 16:42:18,453 DEBUG org.springframework.transaction.interceptor.RuleBa sedTransactionAttribute [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Winning rollback rule is: RollbackRule with pattern [Exception]

    2005-11-24 16:42:18,469 DEBUG org.springframework.transaction.interceptor.Transa ctionInterceptor [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] Invoking rollback for transaction on com.roche.dss.affiliatedatacapture.service.ReportS ervice.save due to throwable [org.springframework.orm.hibernate3.HibernateSystem Exception: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.roche.dss.extreportdomainmodel.Report#23534]]
    -------------------------------------------------------------------------

    Please help!!.

    Thanks
    Keerthi

  • #2
    Take a look at http://www.hibernate.org/116.html#A7
    In Hibernate3, you can try using the merge() operation, which does not reattach the passed instance.

    Comment


    • #3
      Thanks for your response.

      I read some more about merge() and I suppose you need to have version/timestamp column for merge() to work properly.

      http://www.hibernate.org/hib_docs/v3...laration-class

      I did try it out anyway and now I can't even save it the first time. I get

      2005-11-25 10:44:33,511 ERROR com.roche.dss.affiliatedatacapture.web.action.LogE xceptionAction [ExecuteThread: '12' for queue: 'weblogic.kernel.Default'] Caught org.springframework.orm.hibernate3.HibernateSystem Exception: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.roche.dss.extreportdomainmodel.Patient.setConf irmAgeDiff; nested exception is org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.roche.dss.extreportdomainmodel.Patient.setConf irmAgeDiff
      org.springframework.orm.hibernate3.HibernateSystem Exception: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.roche.dss.extreportdomainmodel.Patient.setConf irmAgeDiff; nested exception is org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.roche.dss.extreportdomainmodel.Patient.setConf irmAgeDiff
      org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.roche.dss.extreportdomainmodel.Patient.setConf irmAgeDiff
      at org.hibernate.tuple.PojoTuplizer.setPropertyValues WithOptimizer(PojoTuplizer.java:203)
      at org.hibernate.tuple.PojoTuplizer.setPropertyValues (PojoTuplizer.java:173)
      at org.hibernate.persister.entity.BasicEntityPersiste r.setPropertyValues(BasicEntityPersister.java:2919 )
      at org.hibernate.event.def.DefaultMergeEventListener. copyValues(DefaultMergeEventListener.java:248)
      at org.hibernate.event.def.DefaultMergeEventListener. entityIsTransient(DefaultMergeEventListener.java:1 50)
      at org.hibernate.event.def.DefaultMergeEventListener. onMerge(DefaultMergeEventListener.java:104)
      at org.hibernate.impl.SessionImpl.merge(SessionImpl.j ava:544)
      at org.hibernate.engine.Cascades$6.cascade(Cascades.j ava:176)
      at org.hibernate.engine.Cascades.cascadeAssociation(C ascades.java:771)
      at org.hibernate.engine.Cascades.cascade(Cascades.jav a:720)
      at org.hibernate.engine.Cascades.cascade(Cascades.jav a:847)
      at org.hibernate.event.def.DefaultMergeEventListener. cascadeOnMerge(DefaultMergeEventListener.java:264)
      at org.hibernate.event.def.DefaultMergeEventListener. entityIsTransient(DefaultMergeEventListener.java:1 49)
      at org.hibernate.event.def.DefaultMergeEventListener. onMerge(DefaultMergeEventListener.java:104)
      at org.hibernate.event.def.DefaultMergeEventListener. onMerge(DefaultMergeEventListener.java:54)
      at org.hibernate.impl.SessionImpl.merge(SessionImpl.j ava:535)
      at org.hibernate.impl.SessionImpl.merge(SessionImpl.j ava:539)
      at org.springframework.orm.hibernate3.HibernateTempla te$23.doInHibernate(HibernateTemplate.java:725)
      at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:358)
      at org.springframework.orm.hibernate3.HibernateTempla te.merge(HibernateTemplate.java:722)
      at com.roche.dss.extreportdomainmodel.dao.hibernate.R eportDAOHibernate.saveOrUpdateReport(ReportDAOHibe rnate.java:46)
      at com.roche.dss.affiliatedatacapture.service.impl.Re portServiceImpl.save(ReportServiceImpl.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:292)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:155)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :122)
      at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:96)
      at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :144)
      at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:174)
      at $Proxy42.save(Unknown Source)
      at com.roche.dss.affiliatedatacapture.web.action.Save ReportAction.executeValidToken(SaveReportAction.ja va:71)
      at com.roche.dss.affiliatedatacapture.web.action.Sync hroAction.execute(SynchroAction.java:40)
      at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
      at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
      at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
      at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:415)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:760)
      at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
      at weblogic.servlet.internal.ServletStubImpl$ServletI nvocationAction.run(ServletStubImpl.java:996)
      at weblogic.servlet.internal.ServletStubImpl.invokeSe rvlet(ServletStubImpl.java:419)
      at weblogic.servlet.internal.TailFilter.doFilter(Tail Filter.java:28)
      at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:27)
      at com.roche.dss.security.navigation.LoginFilter.doFi lter(LoginFilter.java:192)
      at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:27)
      at weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.run(WebAppServletContext.java :6458)
      at weblogic.security.acl.internal.AuthenticatedSubjec t.doAs(AuthenticatedSubject.java:321)
      at weblogic.security.service.SecurityManager.runAs(Se curityManager.java:118)
      at weblogic.servlet.internal.WebAppServletContext.inv okeServlet(WebAppServletContext.java:3661)
      at weblogic.servlet.internal.ServletRequestImpl.execu te(ServletRequestImpl.java:2630)
      at weblogic.kernel.ExecuteThread.execute(ExecuteThrea d.java:219)
      at weblogic.kernel.ExecuteThread.run(ExecuteThread.ja va:178)
      Caused by: net.sf.cglib.beans.BulkBeanException
      at com.roche.dss.extreportdomainmodel.Patient$$BulkBe anByCGLIB$$4559e7d0.setPropertyValues(<generated>)
      at org.hibernate.tuple.PojoTuplizer.setPropertyValues WithOptimizer(PojoTuplizer.java:200)
      ... 53 more
      Caused by: java.lang.NullPointerException


      The property that it is trying to set and failing is a boolean.

      Comment


      • #4
        What's your mapping? Btw, this problem has been discussed a lot of times on this and HB forums.

        Comment

        Working...
        X