Resolves a different object with the same identifier value is already associated with the session

Source: Internet
Author: User
Tags sessions
This.gethibernatetemplate (). Clear ();//In One-to-many, many-to-many times, this error is often encountered: a different object with the same identifier value is Already associated with the session 
    Gethibernatetemplate (). Update (user);


Recommended articles:

This problem, I assign the object to null, it is also very useful. In my system, the update is good, but the insertion is not useful. Before the change, is inserted easy to use, and the update is not used. It was a problem in the system, but the catch just caught, from but, did not print out the log, Finding the reason is hard to find. When you are in a high database, it is best to print out the information in the exception capture. Org.springframework.orm.hibernate.hibernatesystemexception:a different object With the same identifier value is already associated with the session:4443398, of class:com.onewaveinc.media.cms.entity . Syncimportfolder; Nested exception is net.sf.hibernate.nonuniqueobjectexception:a different object with the same identifier value was Alrea
Dy associated with the session:4443398, of Class:com.onewaveinc.media.cms.entity.SyncImportFolder Net.sf.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with T He session:4443398, the Class:com.onewaveinc.media.cms.entity.SyncImportFolder at Net.sf.hibernate.impl.SessionImpl.doSave (sessionimpl.java:852) at Net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier (sessionimpl.java:790) at Net.sf.hibernate.impl.SessionImpl.save (sessionimpl.java:749) at ORg.springframework.orm.hibernate.hibernatetemplate$9.doinhibernate (hibernatetemplate.java:555) at Org.springframework.orm.hibernate.HibernateTemplate.execute (hibernatetemplate.java:363) at Org.springframework.orm.hibernate.HibernateTemplate.save (hibernatetemplate.java:552) at Com.onewaveinc.media.cms.dao.impl.SyncFolderHibernateDao.insertHavaIndex (syncfolderhibernatedao.java:31) at Com.onewaveinc.media.cms.manager.SyncFolderManager.doImportSyncFolder (syncfoldermanager.java:193) at Com.onewaveinc.media.cms.manager.SyncFolderManager.insertHavaIndex (syncfoldermanager.java:107) at Com.onewaveinc.media.cms.manager.SyncFolderManager.importSyncFolderList (syncfoldermanager.java:607) at Com.onewaveinc.media.cms.web.SyncFolderImportAction.post (syncfolderimportaction.java:41) at Com.onewaveinc.media.common.web.HttpMethodAction.execute (httpmethodaction.java:36) at Org.apache.struts.action.RequestProcessor.processActionPerform (requestprocessor.java:421) at Com.onewaveinc.media.web.struts.MediaRequeStprocessor.processactionperform (mediarequestprocessor.java:51) at Org.apache.struts.action.RequestProcessor.process (requestprocessor.java:226) at Org.apache.struts.action.ActionServlet.process (actionservlet.java:1164) at Org.apache.struts.action.ActionServlet.doPost (actionservlet.java:415) at Javax.servlet.http.HttpServlet.service ( httpservlet.java:709) at Javax.servlet.http.HttpServlet.service (httpservlet.java:802) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:237) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:157) at Org.ajaxanywhere.AAFilter.doFilter (aafilter.java:41) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:186) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:157) at Com.onewaveinc.appcommon.security.web.utils.SecurityFilter.doFilter (securityfilter.java:89) at Org.apache.catalina.core.ApplicationFiLterchain.internaldofilter (applicationfilterchain.java:186) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:157) at Org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal ( opensessioninviewfilter.java:174) at Com.onewaveinc.media.common.web.SpringHibernateSessionFilter.doFilterInternal ( SPRINGHIBERNATESESSIONFILTER.JAVA:50) at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( onceperrequestfilter.java:76) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:186) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:157) at Com.onewaveinc.media.web.struts.StrutsValidateFilter.doFilter ( strutsvalidatefilter.java:38) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:186) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:157) at Com.onewaveinc.appcommon.utiLs.web.SetCharacterEncodingFilter.doFilter (setcharacterencodingfilter.java:168) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:186) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:157) at Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:214) at Org.apache.catalina.core.StandardValveContext.invokeNext (standardvalvecontext.java:104) at Org.apache.catalina.core.StandardPipeline.invoke (standardpipeline.java:520) at Org.apache.catalina.core.StandardContextValve.invokeInternal (standardcontextvalve.java:198) at Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:152) at Org.apache.catalina.core.StandardValveContext.invokeNext (standardvalvecontext.java:104) at Org.apache.catalina.core.StandardPipeline.invoke (standardpipeline.java:520) at Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:137) at Org.apache.catalina.core.StandardValvecontext.invokenext (standardvalvecontext.java:104) at Org.apache.catalina.valves.ErrorReportValve.invoke ( errorreportvalve.java:118) at Org.apache.catalina.core.StandardValveContext.invokeNext (Standardvalvecontext.java : 102) at Org.apache.catalina.core.StandardPipeline.invoke (standardpipeline.java:520) at Org.apache.catalina.core.StandardEngin.ve.invoke (standardengin.ve.java:109) at Org.apache.catalina.core.StandardValveContext.invokeNext (standardvalvecontext.java:104) at Org.apache.catalina.core.StandardPipeline.invoke (standardpipeline.java:520) at Org.apache.catalina.core.ContainerBase.invoke (containerbase.java:929) at Org.apache.coyote.tomcat5.CoyoteAdapter.service (coyoteadapter.java:160) at Org.apache.coyote.http11.Http11Processor.process (http11processor.java:799) at Org.apache.coyote.http11.http11protocol$http11connectionhandler.processconnection (Http11Protocol.java:705) at Org.apache.tomcat.util.net.TcpWorkerThread.runIt (pooltcpendpoint.java:577) at Org.apache.tomcat.util.threaDs. Threadpool$controlrunnable.run (threadpool.java:683) at Java.lang.Thread.run (thread.java:534) a different object with The same identifier value is already associated with the session a classic hibernate error: A different object with the same identi Fier value was already associated and the session XXXX hibernate3.0 above using merge () to merge the same object in two sessions specific to my own code is public objec
  T GetDomain (Object obj) {gethibernatetemplate (). Refresh (obj);
  return obj;
  public void Deletedomain (Object obj) {obj = Gethibernatetemplate (). Merge (obj);
  Gethibernatetemplate (). Delete (obj); Resolves a different object with the same identifier value is already associated with the session error I've met two times and I've never found a good solution, the cause of this error is believed to be known, because there are two identical identities in the same session in Hibernate but different entities, and this error is reported when the Saveorupdate (object) operation is run. Oh, you may say, you say so without saying nothing different, I admit that, oh, I do not know why the specific cause of this error, otherwise it will not be a long time to solve, now, give a temporary solution, to the same as me, no way to find the source of the people a way to continue to carry out (of course, yes , just not from the cause of the problem. In fact, it's easy to solve this issue, only need to do Session.clean () operation can be solved, but you are in the clean operation after the Saveorupdate (object)operation, it is possible to quote "Found two representations of same collection", I have looked for a lot of information, there is no good explanation, which this article helps the most [url]http://

Opensource.atlassian.com/projects/hibernate/browse/hhh-509[/url]. Finally, it can be solved by the Session.refresh (object) method, noting that Session.refresh (object) is not available when object is not already in the database

Because the refresh is to hibernate the object from the session, if you do not have it, you will have an error, so you need to judge before you use Saveorupdate (object) Of course, the easiest way to solve this problem is to use the merge () method with the hibernate inside itself.

However, I still feel very uncomfortable with the very use of the software itself (and Saveorupdate (), Save (), update () when encountering problems. Later I also found that this error often occurs in One-to-many mappings and many-to-many mappings, please be careful when using one-to-many and many-to-many mappings some Hibernate difficult exceptions and handling 1, a different object with the same identifier Val

  UE is already associated with the session.

  Error reason: There are two identical identities in the same session in Hibernate but different entities. Workaround one: Session.clean () PS: If the Saveorupdate (object) is changed after the clean operation, it is possible to quote "Found two representations of same

  Collection "Exception. Workaround Two: Session.refresh (object) PS: Cannot use Session.refresh when object is not an existing data in the database

  Because the method is to hibernate the object from the session, if it does not, it will be an error, so you need to judge before you use Saveorupdate (object). Solution SideMethod Three: Session.merge (object) Ps:hibernate inside the method, recommended use.

  2. Found two representations of same collection error reason: See 1. Workaround: Session.merge (object) Two exceptions often appear in One-to-many mappings and many-to-many mappings in a different object with the same identifier value is already associ Ated with the session a classic hibernate error: A different object with the same identifier value is already associated with the S ession xxxx hibernate3.0 above uses merge () to merge the same object in two sessions specific to my own code is public object GetDomain (object obj) {Gethibernatetem
  Plate (). Refresh (obj);
  return obj;
  public void Deletedomain (Object obj) {obj = Gethibernatetemplate (). Merge (obj);
  Gethibernatetemplate (). Delete (obj); ==================== I'm a split line =================== in fact, my solution is to rearrange obj, and note that the red Word part of public Serializable save (Object
  PersistentObject) throws Daoexception {try {session session = This.opensession ();
  BeginTransaction ();
  PersistentObject = Session.merge (PersistentObject);
  Serializable id = session.save (persistentobject); if (autocommit)
  CommitTransaction ();
  return ID;
  catch (Hibernateexception ex) {Log.error ("Fail to save PersistentObject", ex);
  throw new Daoexception ("Fail to save PersistentObject", ex);
  finally {if (autoclosesession) closesession ();
 }
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.