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.  [ 2 posts ] 
Author Message
 Post subject: Collection lazy loading
PostPosted: Tue Apr 14, 2009 6:26 pm 
Newbie

Joined: Tue Apr 14, 2009 6:07 pm
Posts: 1
I have a User table and a Group table. Their relation is a simple many-to-many one, since I am eventually interested in things like:

user.groups() // groups the user belongs to
group.users() // members of this group

I also have a UserDAO with basic methods (persist, update, etc). I would like to load a User and eventually let hibernate lazy-load their groups if needed. The problem is that the following does not work since the object is detached:

Code:

test:
User user = UserDAO.getUser("someone");
Set<Group> groups = user.getGroups(); --> does not work

UserDAO.java
   public static User getUser(String username) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();

      User aUser = (User)session.createQuery("from User user where user.userName= :userName")
         .setParameter("userName", username)
         .uniqueResult();
      
       session.getTransaction().commit();

       return aUser;
   }


The documentation says that the call to getGroups() has to be before the session is closed. The problem is that I only know if I will need to load this information when I am in the business logic layer of my application, so I don't have direct access to hibernate sessions neither do I want to go back and deal with DB stuff once I've already used my DAO and loaded my object.

The alternative would be to turn lazy loading off but that is not a viable solution when I have lots of objects with lots of collections.

This is a basic issue so I'm guessing there must be a more solid solution to this. How is this usually solved?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 9:40 pm 
Newbie

Joined: Fri Mar 27, 2009 4:29 pm
Posts: 7
I'm no expert yet, but here's what I ran across recently that I think would help you.

http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#d0e1177

See section 2.2.5.5 Association fetching - they recommend Lazy loading and using JPA QL for particular queries. I think you can define that using an annotation, like javax.persistenceNamedQuery
Quote:
@NamedQuery(
name="findAllCustomersWithName",
query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)


which supports vendor-specific hints in the 'hints' attribute. I would guess that is where loading is overridden, but you'd have to search on that.

HTH -


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