-->
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.  [ 8 posts ] 
Author Message
 Post subject: hang on to lazy loading, but how?
PostPosted: Tue Nov 14, 2006 4:56 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
hi,

I have following mapping file:

Code:
<hibernate-mapping package="com.skopco.pronocl.data">
    <class name="Team" table="TEAM">
     ...
    <set name="users" table="USERTEAM">
         <key column="TEAM_ID"/>
        <many-to-many column="USER_ID" class="User" />
    </set>
...

Basically a team contains users.
Now, I'm loading all teams and for each team get all it's users:

Code:
List teams = dao.getTeams();
for (Iterator iterator = teams.iterator(); iterator.hasNext();)
{
   Team t = (Team) iterator.next();
   Set s = t.getUsers();
         
   for (Iterator iter2 = s.iterator(); iter2.hasNext();)
   {
      User u = (User) iter2.next();
...


I'll stop right there because this already throws an exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.skopco.pronocl.data.Team.users, no session or session was closed

if I change the mapping file like this:

Code:
<set name="users" table="USERTEAM" [b]lazy="false"[/b]>


it works fine (obviously).

My question is, if I want to hang on to lazy loading (teams are loaded many times, it's users are only required now and then) then how can I load the team's users explicitly?
I could create an HQL-query which return the users by team-id but I'm sure there a 'hibernate-solution' for this.

many thanks for any help!

Stijn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 5:36 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Y E S you definately want to hang to lazy loading. This exception occurred because you accessed a lazy collection after closing your session.

You have the following options:
Within the "lifetime" of your session you can preload the lazy collection with Hibernate.initialize(team.getUsers)

Again within the liftime of the session you can eagerly load the teams users with a left join fetch hql query

from Team t left join fetch t.users

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 5:54 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
MikePloed wrote:
Y E S you definately want to hang to lazy loading. This exception occurred because you accessed a lazy collection after closing your session.


Thanks for your reply.

Sorry if this is a stupid question (hib-beginner) but when exactly is a session closed?
In this case I never do a close (or tx.commit); I load the teams and then try to get the users without closing the session. Or is the latter done automatically somehow? If so, when?

don't worry, won't forget your credits


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 6:00 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Can you please post the code of dao.getTeams() .. I think you close the session in there

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 6:17 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
MikePloed wrote:
Can you please post the code of dao.getTeams() .. I think you close the session in there


Code:
public class TeamManager
{
   private TeamDAO dao = new TeamDAO();
   
   public List getAll()
   {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
       
        session.beginTransaction();

        return dao.getAll(session);
}


Code:
public class TeamDAO
{
   public List getAll(Session session)
   {
      return session.createQuery("from Team").list();
   }
}


thanks,
Stijn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 6:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 8:08 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
christian wrote:
http://hibernate.org/42.html


thanks,

I'll try out the session-per-conversation pattern but I still don't see when the session is closed in the code above. I never do a commit or flush between loading the teams and getting the users from a team?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 8:34 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
Quote:
this might work fine when running with servlets but what about unit-testing? You can't use servlet-filters in that case. My unit-test for this action also fails (same exception).


you may discard this, stupid question

regards,
Stijn


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