-->
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: Sun Jun 15, 2008 3:51 am 
Newbie

Joined: Fri Jun 13, 2008 11:27 am
Posts: 6
I have the delete method:

public boolean deleteCustomerByCustomerId(long customerId) {
Session session=getHibernateTemplate().getSessionFactory().openSession();
session.delete(getCustomerByCustomerId(customerId));
return true;
}


and the getCustomerByCustomerId method is:

public TblCustomerInfo getCustomerByCustomerId(long customerId) {
Session session=getHibernateTemplate().getSessionFactory().openSession();
StringBuffer hql=new StringBuffer("from TblCustomerInfo where customerId=:customerId");
Query q=session.createQuery(hql.toString());
q.setParameter("customerId", customerId);
return (TblCustomerInfo)q.uniqueResult();

}


When i call the delete method it give me a error:Illegal attempt to associate a collection with two open sessions


How to slove this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 5:39 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You need to implement some form of session management to ensure you stick to the basic rules:
1. open session
2. start transaction
3. do your work
4. commit transaction
5. close session

What you did was this:
open session1
do work
open session2
do work on session1's objects

How you manage your sessions depends very much on your environment: standalone, app server, spring, etc. Check out 1.2.5 in the the tutorial for basic management using HibernateUtil.
http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 9:42 am 
Newbie

Joined: Fri Jun 13, 2008 11:27 am
Posts: 6
Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 10:36 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I'm not sure how you are managing the session, but you might also want to take a look at the getSession method of the Hibernate Session, as opposed to the openSession method.

The getSession method will look for an existing session in the current thread local, or however it is set up in the hibernate.cfg.xml file. On the other hand, opensession will return you a session that expects you to manage the lifecycle of the session. So, getSession may help extra session objects from being created when perhaps they don't need to be.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.