• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Illegal attempt to associate a collection with two open sessions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am taking data from one table and saving it on other, both the database are exact replica of each other.
So I am working with two hibernate sessions, and everything is working fine.
But when it comes to collection it throws this exception:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:44)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:98)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:333)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at dms2dms.controller.DMS2DMSController.testCopy(DMS2DMSController.java:76)
at dms2dms.controller.DMS2DMSController.main(DMS2DMSController.java:25)



mapping file looks like this....
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="dms2dms.businessobject.DMSTitle" table="TITLE">
<id name="titleID" column="title_id" type="java.lang.Long">
<generator class="assigned" />
</id>
<property name="titleNumber" column="title_number" type="string"/>
<property name="authorisedBy" column="authorisation" type="string"/>
<property name="dateAuthorised" column="date_authorised" type="java.util.Date"/>

<many-to-one name="dmsCase" class="dms2dms.businessobject.DMSCase" column="case_id" cascade="all"/>
<many-to-one name="dmsOverlay" class="dms2dms.businessobject.DMSOverlay" column="overlay_id" cascade="all"/>

<set name="dmsMultiPart" table="multi_part" cascade="all">
<key column="title_id"></key>
<one-to-many class="dms2dms.businessobject.DMSMultiPart"/>
</set>

</class>
</hibernate-mapping>
 
Abhinav Sahai
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it working by copying the Set before saving. Since collection is not synchronous thats why we need to do this acitvity
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm having the same problem that you had. I understand that it's the saving of the set that is the problem but what do you mean by "copying the Set before saving"? Copy it how?

Thanks.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Jboy JW"

Welcome to JavaRanch!

Please update your display name to comply with our naming policy. You can do this here.

Thanks!
[ September 17, 2008: Message edited by: Paul Sturrock ]
 
Jason Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, name updated. I still need help on this issue though.
 
Jason Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind. I understand. You mean actually make a copy of the set in the code and use it to attach to the parent entity. And to make a copy of the set, I had to loop through each item of the set and add them to a new set to assign to the parent. Sweet.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than looping, you could possibly evict the entity, so long as it is not loaded in a lazy fashion.

-Cameron McKenzie
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic