-->
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.  [ 6 posts ] 
Author Message
 Post subject: lazy/non-lazy loading at runtime
PostPosted: Fri Aug 03, 2007 3:30 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 11:11 am
Posts: 24
Location: India
Hibernate version:3.2.4.sp1

Hi guys,
Can i decide lazy/non-lazy loading at runtime, without declaring it in the mapping files?

Supriyo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 10:31 am 
Newbie

Joined: Tue Mar 06, 2007 1:35 pm
Posts: 10
As far as I know, you can't... Once the collection is declared lazy or eager, that will be it's behavior. There's a way of fetching lazy (not extra-lazy) collections, that is calling the collections method size(), forcing hibernate to load it any time you want (within a valid session).

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 1:38 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 11:11 am
Posts: 24
Location: India
Monteiro wrote:
As far as I know, you can't... Once the collection is declared lazy or eager, that will be it's behavior. There's a way of fetching lazy (not extra-lazy) collections, that is calling the collections method size(), forcing hibernate to load it any time you want (within a valid session).



Thanks,
but i know this. I'm asking for is there any way to load a collection in a NON-LAZY manner when it is defined as LAZY in the mapping file. I need this because most of the time my requirement is lazy loading but few times it is not, so i want to take decision at runtime. Is there any way? If not can it be possible to add this feature to next version?

Supriyo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 7:54 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you're loading a single entity you can use HQL or criteria to eager populate a collection.

e.g.
Code:
Long idToLoad = new Long(2);
Query q = s.createQuery("from Cat cat join fetch cat.kittens where cat.id = ?");
q.setParameter(0, idToLoad);
Cat cat = (Cat)q.uniqueResult();


This will populate the kittens collection of Cat using an sql join.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 5:05 pm 
Newbie

Joined: Fri Sep 28, 2007 7:40 pm
Posts: 9
If Kittens has also collections how to eager initialize/over-ride the lazy=true configuration. Can you please help on this

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 5:44 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Try this:
Code:
from Cat cat
join fetch cat.kittens child
join fetch child.kittens
where cat.id = ?


Don't forget that, depending on your data, its sometimes more efficient to issue sub-selects rather than adding another join to the query.


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