-->
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.  [ 11 posts ] 
Author Message
 Post subject: Lazy Loading
PostPosted: Tue Jan 31, 2006 7:17 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
Is there a way to programattically changed lazy loading to full loading and viceversa?

To elaborate, I have mappings in my xml file for lazy loading between a main table and n number of accessory tables. Based on certain scenarios, I would like to either load only the primary table or the primary table and all the n dependant tables.

Any suggestions would be appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:19 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
You can explicitly set the fetch behaviour using criteraQueries
or HQL queries (left join fetch).
Have a look in the Hibernate reference for this.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject: Re: Lazy Loading
PostPosted: Tue Jan 31, 2006 7:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kshreeram wrote:
Is there a way to programattically changed lazy loading to full loading and viceversa?

To elaborate, I have mappings in my xml file for lazy loading between a main table and n number of accessory tables. Based on certain scenarios, I would like to either load only the primary table or the primary table and all the n dependant tables.

Any suggestions would be appreciated.

Thanks

Check out Section 15.5 "Dynamic association fetchin" in the 3.1 docs

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:49 pm 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
Yeah, I know that Criteria's setFetchMode can be used to dynamically change the lazy loading, but with this approach I have to list out all associations within my query and for each of this association I would have to mark the fetch mode. ( I have one primary table and n dependant tables )
If there is another generic alternative where I can just programmatically iterate over all the relations of a particular table ( or say the mappings ) and change the fetch mode, it would be better. Or at a high level if I could say do a full loading of whatever is possible for this criteria - rather than at an individual relationship level...

Please let me know if my question is not clear.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:55 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kshreeram wrote:
Yeah, I know that Criteria's setFetchMode can be used to dynamically change the lazy loading, but with this approach I have to list out all associations within my query and for each of this association I would have to mark the fetch mode. ( I have one primary table and n dependant tables )
If there is another generic alternative where I can just programmatically iterate over all the relations of a particular table ( or say the mappings ) and change the fetch mode, it would be better. Or at a high level if I could say do a full loading of whatever is possible for this criteria - rather than at an individual relationship level...

Please let me know if my question is not clear.

Thanks


This wiki topic doesn't do what your asking but it might address your need.
http://www.hibernate.org/41.html

You can have a base object without the associated tables and another mapping with all the associations eagerly loaded.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 8:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate doesn't have an "inflate all associations" option anywhere. It can't really do that, because it is designed to fully support a massively-connected graph, and there's a very high likelihood of completely consuming all memory if a graph relates all data in your system.

If all of your mapped classes implement a single interface (something like "DatabaseEntity", or even a marker interface like "Inflatable"), then you can use reflection to recurse from a single object, inflating (Hibernate.initialize) all its inflatable members (and collection members). This would be exactly what you are looking for. If you really wanted to do that, I'd recommend requiring a recursion depth as a parameter to that method, or else you'll rapidly run out of memory.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 6:08 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,
hopefully you know the folling syntax:
Code:
fetch all properties


Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 8:11 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can initialize lazy entities, but entities mapped as "lazy=false" can not be made lazy. Probably "lazy=true" is default for this reason. Marker interface will not help ( and you can declare the same thing in mapping).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 11:12 am 
Beginner
Beginner

Joined: Wed Jul 06, 2005 4:51 pm
Posts: 27
Let me elaborate my problem

I have a base class A which maps to table a, dervied classes A1,A2... An which all extend A, use <subclass> element for extension and map to tables a1,a2.... an.

Each such hierarchy (A1.. An) has a set of dependant tables. For eg A1 has dependant tables (a11,a12,.. a1m ), A2 has (a21, a22,... a2m ) and so on.

Between classes A and Ai (i=1..n) I use <subclass> element. I have lazy loading enabled between hierarchies Ai and their dependant tables.
My goal is to either load just hierarcy Ai or do a full loading of Ai and their dependant tables programtically. I can achieve the first goal easily with polymorphism explicit and querying for classes Ai. The second part is my problem.
At least if there is a way to figure out dynamically the relation names of hierarchy Ai, I could probably do a

session.createCriteria(Ai.class).
setFetchMode(getRelationName(a11),FetchMode.EAGER).
setFetchMode(getRelationName(a12),FetchMode.EAGER).
......

I will probably look in that direction to get relation names programtically. Please let me know if there is an easy way to do this or any other alternative way to approach the problem.

I am not worried about any memory constraints.

BTW thanks for all your suggestions.

I will rate you guys all at once once I am done with this problem ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 5:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Sebastian, does fetch all properties do a deep fetch? I've never used it, but from my reading of the docs, it looks like it only forces a fetch of all properties of classes mentioned in the queries. Given a mapping A, lazily referencing B, in turn lazily referencing C, then doesn't "from A fetch all properties" inflate A and B, but not C? If the query was "from A a join a.B b fetch all properties" then C would be eagerly loaded too. The request was for a full graph fetch, so fetch all properties wouldn't be enough (if my understanding is correct).

baliukas, could you have misread either the original question or my response? Marker interfaces will allow custom reflection code to inflate all lazily loaded entities: the code could just find all Methods named get*, and if the return type implements the marker interface, call Hibernate.initialize on it. Obviously it can't make eagerly loaded entities be loaded lazily, which is what I think you've saying.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 7:59 am 
Regular
Regular

Joined: Tue Nov 23, 2004 7:42 am
Posts: 82
Location: London, England
baliukas wrote:
You can initialize lazy entities, but entities mapped as "lazy=false" can not be made lazy. Probably "lazy=true" is default for this reason. Marker interface will not help ( and you can declare the same thing in mapping).


Is there absolutely no way of doing this? After being given some stupid design constraints and not knowing what I know now I've got a heavy method that I could make so much quicker by changing some non-lazy-loaded properties to being lazy loaded.

The app is still stuck in Hibernate2 territory.


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