-->
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: How to implemnt Lazy initialization in Hibernate?
PostPosted: Wed Apr 08, 2009 4:42 pm 
Newbie

Joined: Wed Apr 08, 2009 4:36 pm
Posts: 1
In my non-web Hibernate project:

Code:
public class ImageSet {

    private Set<GeneralImage> referencedImages;

}

ImageSet imageSet = getImageSet("SeriesID");

Set<GeneralImage> images = imageSet.getReferencedImages();


In hibernate configuration file, set lazy="true" and will meet exception: "LazyInitializationException"

Because in getImageSet method, close the session

Code:
public static ImageSet getImageSet(String UID){

    Session session = HibernateUtil.getSessionFactory().openSession();

    ImageSet imageSet = findImageSet(UID, session);

    session.close();

    return imageSet;

}


If I set lazy="false", and it will don't show exception, but I want to set lazy="ture", because of performance. I want to separate Dao layer and application layer, and don't want to let hibernate session appears in application layer, so I want to know how to solve this problem? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 09, 2009 8:39 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
When you need to then load your referencedImages, pass the imageSet back into your dao layer. And there you'll need to reassociate imageSet with another open session and then get those referencedImages.


And if you're merely doing it immediately after your initial load like your example shows, why even set it to lazy = "true"

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.