-->
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.  [ 3 posts ] 
Author Message
 Post subject: Lazy associations impossible?
PostPosted: Thu Aug 31, 2006 12:42 am 
Newbie

Joined: Tue Aug 22, 2006 11:44 am
Posts: 3
Hibernate version:3.1

Sorry for the noob question. But what I'm hoping to achieve is for my many-to-one associations to be fetched only if those properties are read.

I see options are proxy, no-proxy and false. I've tried all of these and it appears (by looking at the SQL being executed) that any time I load an object ALL of its associations are immediately loaded too. This is a big problem for me when I have deep association hierarchies and am querying for thousands of objects.

Example:

select a from Cat a

This doesn't just load Cats, it also queries for Cat's parent, uncle, aunt, species... any association mapped with it. In many cases I don't want all those, they just slow down my results (but I want them populated when/if they're read).

I searched the forums, but it seems people only talk about different ways of fetching associations but I've been unable to find exactly this. Is what I'm trying to do possible? If not, what's a good strategy to for my situation?

Thanks


Top
 Profile  
 
 Post subject: Lazy initialization
PostPosted: Fri Sep 01, 2006 3:29 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
What you need is called lazy collection fecthing. Associations are transparently fetched on first access. This is the default behaviour in Hibernate.
What you realised is called eager fetching. It happens if you specify your associations or classes using the property lazy="false". Please note that there is even a property "default-lazy" at the "mapping" tag.

Lazy initializations are only applicable as long the session where you fetched the object is still open.
See also: Hibernate Reference Documentation Chapter 19.1

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 4:10 am 
Newbie

Joined: Tue Aug 22, 2006 11:44 am
Posts: 3
Thanks for your response. You must've just beat me here by a matter of a few minutes ;-)
I've spent a large part of the day frustrated by this. Of course it turned out to be something real small. I just couldn't understand why, no matter what I did, my many-to-ones would be eagerly fetched. Through lots of trial and error, I discovered that having,
not-found=true,
causes the lazy fetching to be quietly turned off.

Of course, once I identified the problem I knew which key words to search on and found a discussion on it here.

http://forum.hibernate.org/viewtopic.php?t=945431&highlight=proxy+notfound

Thanks again for your effort.


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