-->
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.  [ 4 posts ] 
Author Message
 Post subject: Lazy instantiation - what triggers it?
PostPosted: Fri Aug 12, 2005 6:14 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I am trying to debug an area in my application where I get an out of memory error when an object is loaded. From switching on the SQL trace, I find to my surprise that Hibernate (2.1.8) is issuing a SELECT for the related many records, even though I have defined 'lazy="true"' for these one-to-many relations. Obviously something somewhere is doing whatever it is which tells Hibernate to load the related many. So what would that be? Would it always be a call like 'getChildren()'?


Top
 Profile  
 
 Post subject: Look at the setter...
PostPosted: Fri Aug 12, 2005 6:30 am 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
For instace if the setter-method of the lazy-property uses any value, the lazy-init triggers immideately.

I had a similar problem where I wanted the property to be a SortedSet. I then coded the setter like this :
Code:
public void setChildren(Set children){
if (children != null) {
this.children = new TreeSet(children);
}
}


This made all clidren to be loaded immideately, no matter if the hibernate-mapping said lazy or not....


Top
 Profile  
 
 Post subject: Re: Lazy instantiation - what triggers it?
PostPosted: Fri Aug 12, 2005 6:35 am 
Newbie

Joined: Fri Aug 12, 2005 3:55 am
Posts: 11
J2R wrote:
Would it always be a call like 'getChildren()'?

In ordinary situation a lazy collection initialized when you call ANY method from it. But, I think, it can be initialized when you use proxy for object and call 'getChildren()'.


Top
 Profile  
 
 Post subject: Re: Look at the setter...
PostPosted: Fri Aug 12, 2005 6:53 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 5:09 am
Posts: 22
frodeh wrote:
For instace if the setter-method of the lazy-property uses any value, the lazy-init triggers immideately.

I had a similar problem where I wanted the property to be a SortedSet. I then coded the setter like this :
Code:
public void setChildren(Set children){
if (children != null) {
this.children = new TreeSet(children);
}
}


This made all clidren to be loaded immideately, no matter if the hibernate-mapping said lazy or not....


Don't do that, the getter should return the instance provided by the setter or Hibernate will treat it as a new Collection with new children. This is clearly stated in the docs: what you're doing is called derefencing the collection. Hibernate uses its own implementation of the standard Collection interfaces, so you can't use your own implementation class. Just stick to using the interface. If you want the collection to be ordered, use a List or adjust your mapping (I think Hibernate supports sorted sets, but don't know how exactly from the top of my head).

Joris


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