-->
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.  [ 5 posts ] 
Author Message
 Post subject: Illegal attempt to associate a collection with two opensess
PostPosted: Fri Jan 16, 2009 2:23 pm 
Newbie

Joined: Fri Jan 16, 2009 8:40 am
Posts: 7
Hi

While using unidirectional one to many associations ( using a set ), we are getting error as :
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions during update operation ( after doing a select using the same session ) .

Please let me know the reason to this & solution ASAP.

Thanks
Nisha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 1:37 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
You are managing your Hibernate sessions incorrectly in your program. That set should be modified in the context of the same session from which you obtained it. Don't manipulate that set elsewhere and then try to reinsert it into the parent object, for example, Hibernate won't allow that.

How do you obtain your Hibernate sessions?
Can you post the code that obtains the parent object, and the code that uses the set?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 3:22 am 
Newbie

Joined: Fri Jan 16, 2009 8:40 am
Posts: 7
This is my utility class used to obtain hibernate session.

package financing.tools.dgs.ws.hibDB;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtility{
private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}


public HibernateUtility(){

}



public static SessionFactory getSessionFactory() {
return sessionFactory;
}


public void insert(Object beanClass){
Session session = getSessionFactory().openSession();
session.save(beanClass);

}

public void update(Object bean){
Session session = getSessionFactory().openSession();
session.merge(bean);
session.flush();

}

public Object select(Serializable uniqueIdentifier,Class beanclass){
Object beanObject = null;
Session session = getSessionFactory().getCurrentSession();
beanObject =session.load(beanclass, uniqueIdentifier);
session.flush();
return beanObject;
}

public void delete(Object beanClass){
Session session = getSessionFactory().getCurrentSession();
session.delete(beanClass);
session.flush();
}

public Object select1(Serializable uniqueIdentifier,Class beanclass){
Object beanObject = null;
Session session = getSessionFactory().openSession();
beanObject =session.get(beanclass, uniqueIdentifier);
session.flush();
return beanObject;
}




}




Now i am calling this utility in my HandlerHibernate class code.The snippet of calling utility class is pasted here. I am using JTA transaction for this.

UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
try{
tx.begin();
weblog1 = (Weblog)hb.select1(uniqueKey, Weblog.class) ;
hb.delete(weblog1);
tx.commit();
}catch(RuntimeException e){
tx.rollback();
e.printStackTrace();
}

Code:

My mapping file for tables:

   <class name="financing.tools.dgs.ws.hibDB.Weblog" table="GCPS.WEB_SERVICES_LOG1"
    dynamic-update="true">
        <composite-id
        name="composite_key"
        class="financing.tools.dgs.ws.hibDB.Composite" >
       
        <key-property name="application_Name" column="APPLICATION_NAME" type="string" />
        <key-property name="document_Number" column="DOCUMENT_NUMBER" type="string"/>
        <key-property name="create_Timestamp" column="CREATE_TIMESTAMP" type="timestamp"/>         
         
       </composite-id>       
        <property name="service_Request_Response" type="string" column="SERVICE_REQUEST_RESPONSE"/>
        <property name="fault_Occured" type="character" column="FAULT_OCCURRED"/> 
       
       
        <set name="websoap"  cascade="delete" lazy="true" >
        <key not-null="true">
        <column  name= "APPLICATION_NAME" />
         <column name="DOCUMENT_NUMBER"/>   
         <column name="CREATE_TIMESTAMP"/>           
        </key>
         <one-to-many class="financing.tools.dgs.ws.hibDB.Websoap1" />
       </set>
       
       <set name="webattachments"  cascade="all-delete-orphan" lazy="true">
        <key not-null="true">
        <column  name= "APPLICATION_NAME" />
         <column name="DOCUMENT_NUMBER"/>   
         <column name="CREATE_TIMESTAMP"/>           
        </key>
         <one-to-many class="financing.tools.dgs.ws.hibDB.Webattachments" />
       </set>
        <query name="byServiceName"><![CDATA[from financing.tools.dgs.ws.hibDB.Weblog as log where log.service_Request_Response = ?]]></query>
       
    </class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 3:51 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
The problem is that you want to have a contextual sessions bound to the jta transaction but use inappropriate SessionFactory method for that.

Just use getSessionFactory().getCurrentSession() instead of getSessionFactory().openSession() at select1().


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 10:14 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Denis is correct. If you open a fresh session every time, the new session doesn't know about the session that had previously retrieved your set.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.