-->
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: Illegal attempt to associate a collection with two open sess
PostPosted: Tue Dec 02, 2003 10:08 pm 
Newbie

Joined: Wed Sep 24, 2003 3:30 pm
Posts: 7
Hi All,

I'm new to Hibernate & am having trouble with deleting an object that is part of a Parent-Child relationship. Yes, I've read the chapters in the documentation. But, obviously, I'm still missing something! :)

I have an object "Group" that can has two types of children: Users and other Groups. (This is kind of like a directory that can have both files & other directories as children.) Adding and updating Groups is not a problem. When I try to delete a Group that is a child of another group, I get a foreign key constraint failure. Which is makes sense. So I modified my code to remove the child from the parent relationship and remove all it's children too. Unfortunately, when I try this, I get the following exception when I try to update the parent : HibernateException: Illegal attempt to associate a collection with two open sessions. I don't understand why this would be happening.

The exception seems to be throw at line 1104 of SessionImpl:
Code:
         
          // a collection loaded in the current session
          // can not possibly be the collection belonging
          // to the entity passed to update()
          removeCollection(persister, id);



My code & mapping xml follow.

Any help would be greatly appreciated!!! :)

Linda:)

The session returned below uses the ThreadLocal pattern described on the Hibernate webiste.

Code:
public void deleteGroup(UserGroup delGroup) throws Exception
{
        Session session = null;
        Transaction trx = null;
       
        try
        {
            session = Persistence.instance.getSession();
            trx = session.beginTransaction();
       
            java.util.Iterator parents = delGroup.getParents().iterator();
            while(parents.hasNext())
            {
                UserGroup parent = (UserGroup) parents.next();
                parent.removeGroup(delGroup);           
                session.update(parent);
            }
            delGroup.setUsers(null);
            delGroup.setGroups(null);
            delGroup.setParents(null);
            session.delete(delGroup);
            trx.commit();
        }       
        catch (Exception e)
        {
            trx.rollback();
            throw e;
        }
        finally
        {
            session.close();
        }   
}



UserGroup.hmb.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="com.po.review.data.UserGroup"
         table="userGroup"
         persister="net.sf.hibernate.persister.EntityPersister"     
   >
       
     <id name="id"   
         column="id"
         type ="long"
         unsaved-value="-1">       
         <generator class="native"/>
      </id>
      <property name     ="name"               
                column   ="name"
                type     ="string"
                length   ="25"
                not-null ="true"
                unique   ="true"
      />       
      <property name     ="description"
                column   ="description"
                type     ="string"
                length   ="128"
                not-null ="false"
      />
      <set name="users"
             table="group_user">
             <key column="group_id"/>
             <many-to-many class="com.po.review.data.User" column="user_id"/>         
      </set>
      <set name="groups"
             table="group_subgroup">
             <key column="group_id"/>
             <many-to-many class="com.po.review.data.UserGroup" column="subGroup_id"/>         
      </set>
      <set name="parents"
             inverse="true"
             table="group_subgroup">
             <key column="subGroup_id"/>
             <many-to-many class="com.po.review.data.UserGroup" column="group_id"/>
      </set>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 10:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can't associate an object with two open sessions. The message is quite explanatory.


Top
 Profile  
 
 Post subject: Not helpful
PostPosted: Mon Mar 13, 2006 2:07 pm 
Newbie

Joined: Mon Mar 13, 2006 2:03 pm
Posts: 3
You know Gavin, I have read numerous posts in this forum and consistently your answers are rude and not helpful. Perhaps you should just not bother posting a reply if they are as useless as this was....


Top
 Profile  
 
 Post subject: Maybe HttpSession Problem or Threading Issue
PostPosted: Thu Mar 16, 2006 11:15 am 
Newbie

Joined: Tue Jan 31, 2006 8:20 pm
Posts: 2
Hi,

actually I have seen this Exception in my web application too. But to be honest I can't remember how I solved it.

(In case your app is also a web application)
I think it was either the problem of using an old HttpSession
or with multiple threads accessing the same HttpSession.

I had both problems in my webapp. Unfortunately I don't remember
which one solved the problem, but I am quite sure that in my case
one of the two was the problem.

Problem 1 I solved by:

checking each HttpServletRequest if it called my initial StartAction
and if that's the case see if an HttpSession already exists (by calling
request.getSession(false) and checking if it is not null, then calling invalidate on it)

Problem 2 I solved by using a synchronization Filter I found
on this Website:
http://www.onjava.com/onjava/2004/03/24 ... ilter.java

Hope it helps...

RĂ¼diger.


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.