-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session management of Foreign key relationship
PostPosted: Tue Jul 13, 2004 7:33 am 
Newbie

Joined: Mon May 10, 2004 3:50 am
Posts: 1
I have 2 table with foreign key constraint enabled. When I use hibernate sync. to generate the schema mapping file, it help me to add the following tag to ensure a list in parent table to access child record:
Quote:
<set inverse="true" name="CourseTemplateFrameSet">
<key column="id" />
<one-to-many class="CourseTemplateFrame" />
</set>

However, then in my code I load an instance of parent object, then save it. When I saving the object, it complaint about : "Illegal attempt to associate a collection with two open sessions"

The java code of load is:
Quote:
session = getInstance().openSession(con);
session.load(st, st.getId()); // st is parent
TOP.read(session); // child record
LEFT.read(session);
BUTTON.read(session);
session.flush();
session.disconnect();

The java code of save is:
Quote:
session = getInstance().openSession(con);
session.update(st);
TOP.save(session);
LEFT.save(session);
BUTTON.save(session);
session.flush();
session.disconnect();

I think the exception is cause by I am not suppose to save / load the child record outside hibernate if I have put the above tag, right? I guess it should have better exception message about this, rather than "Illegal attempt to associate a collection with two open sessions", right?[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 7:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It looks like your code is leaking Sessions, it certainly doesn't look correct. You should reconnect(), not open a new Session, if you disconnect(). Don't forget to close() it at some point. This is more advanced than you might realize, don't use disconnect()/reconnect() unless you are implementing long running Application Transactions with a single Session. See Hibernate in Action for more info.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 7:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
your code is also leaking transaction...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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