wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Illegal attempt to associate a collection with two open sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Illegal attempt to associate a collection with two open sessions " Watch "Illegal attempt to associate a collection with two open sessions " New topic
Author

Illegal attempt to associate a collection with two open sessions

Abhinav Sahai
Greenhorn

Joined: May 21, 2008
Posts: 12
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

Joined: May 21, 2008
Posts: 12
I got it working by copying the Set before saving. Since collection is not synchronous thats why we need to do this acitvity
Jason Wilson
Greenhorn

Joined: Sep 17, 2008
Posts: 3
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.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jason Wilson
Greenhorn

Joined: Sep 17, 2008
Posts: 3
Sorry, name updated. I still need help on this issue though.
Jason Wilson
Greenhorn

Joined: Sep 17, 2008
Posts: 3
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.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Rather than looping, you could possibly evict the entity, so long as it is not loaded in a lazy fashion.

-Cameron McKenzie
 
 
subject: Illegal attempt to associate a collection with two open sessions
 
Similar Threads
NullPointerException when run stand alone program
Problem in mapping
What is "object is not an instance of declaring class" error in hibernate?
many side of one to many not being populated
Error on inserting a new record to table: