-->
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: MULTIPLE EAGER RELATIONSHIPS OR ACCESSING LAZY COLLECTIONS?
PostPosted: Wed Aug 08, 2007 2:02 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hey,

Basically my problem is when you have multiple relationships in one entity, and the fetch type of all them is eager then I get the error relating to how hibernate cannot fetch multiple bags at once. But if I switch some to lazy I cannot get access to the collection and it says no session or session is closed. The question is how do I go about getting access to a lazily loaded collection if I must switch some from fetch type eager to lazy?

thanks in advance for your assistance


Hibernate version:
hibernate 3.2
Mapping documents:
RELATIONSHIP 1:(SWITCHING THIS ONE BETWEEN EAGER AND LAZY)
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)

RELATIONSHIP 2:
@ManyToOne(fetch=FetchType.EAGER, targetEntity=pack.Region.class)
@JoinColumn(name="REGION_FK", referencedColumnName="Area", nullable=true)

RELATIONSHIP 3:
@OneToOne(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
IF THERE ARE MULTIPLE RELATIONSHIPS WITH FETCH TYPE EAGER, THE ERROR IS:

Caused by: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags

IF I CHANGE ONE OF THE RELATIONSHIPS TO FETCH TYPE LAZY AND TRY TO ACCESS THE COLLECTION I GET THE FOLLOWING ERROR;

org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: pack.singles, no session or session was closed

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject: Any examples?
PostPosted: Wed Aug 08, 2007 3:32 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
The link here is basically my problem, the session is closed before the lazy loaded collections can be accessed. Are there any examples anywhere of how to load the lazy collections in the query before the session closes, because that seems to be where this is headed.

thanks in advance

http://www.hibernate.org/hib_docs/refer ... mance.html

Section 19.1.1

s = sessions.openSession();
Transaction tx = s.beginTransaction();

User u = (User) s.createQuery("from User u where u.name=:userName")
.setString("userName", userName).uniqueResult();
Map permissions = u.getPermissions();

tx.commit();
s.close();

Integer accessLevel = (Integer) permissions.get("accounts"); // Error!


Top
 Profile  
 
 Post subject: fetch command
PostPosted: Wed Aug 08, 2007 4:33 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Hey,

Here are some good resources for anybody who might run into a similar problem. Explains how to use fetch to load the lazy collections which resolves the problem.

http://gary.teaching.googlepages.com/Hi ... rial09.pdf
http://www.realsolve.co.uk/site/tech/or ... rmance.php


Top
 Profile  
 
 Post subject: thanks
PostPosted: Fri Sep 21, 2007 2:43 pm 
Newbie

Joined: Fri Sep 21, 2007 2:41 pm
Posts: 1
Thank you, these links helped me. I had the same problem. I switched my 'bag' implementations of collections to 'set'.


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.