-->
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.  [ 9 posts ] 
Author Message
 Post subject: Lazy collection is having problems?
PostPosted: Fri Mar 12, 2004 4:47 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi all,

I'm having problems with my lazy one-to-many relationships...

I mark them as lazy="true" but when I load my parent, all my childs with lazy="true" are loaded too.

<set
name="child"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>

<key
column="column"
/>

<one-to-many
class="child"
/>
</set>



I'm using 2.1.1 and tried also with 2.1.2

Is there something I am missing?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 8:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It makes absolutely zero sense to hit the child table to load up the collection and then not also load up the objects in the collection. This is the nature of a one-to-many,


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 3:42 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi Gavin,

I think I didn`t make myself clear....

What I mean is that I load my parent entity and my child collections marked with lazy="true" are being loaded too.

I thought my child collections shouldn`t be being loaded with lazy="true".

Am I missing something wrong? Or maybe my mapping is wrong?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Step through your code. You somewhere access the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 7:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it reminds me a problem i had
look at this: http://forum.hibernate.org/viewtopic.php?t=928298

Finally it was only me who was doing something wrong with eclipse
It's a good way to see logs about lazy loading


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 14, 2004 11:59 am 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Gavin,

my child collections are being initialized in the call to Session.load() or session.find(). I can see the select statements in console.

So the only possible place I may (or I *must* :) ) be calling my collections could be inside... my parent acessors!!

I admit I didn't check it yet, but I will do it tomorrow as the code is in my dev box at my job. I will let you know if I find something. Thanks.


Delpouve,

Thanks for the info. I will do some investigation tomorrow.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 2:19 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Ok,

Sorry for false alarm... :(

It was my parent which was loading the child as I added code in the setter to do the relationship from child to parent:

void setChild(Set childs)
{
for each child <----- this was loading my lazy collections :(
child.parent = this;
}


Is there some clean way I could use to know that the parent is being created by hibernate so that I don't have to initialize my childs?

Or I have to refactor my code so that I don't use the acessors to do the child - parent relationship??

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:23 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
You don't have to set the child's parent at all.

If and when Hibernate loads the child, it will initialize the child's parent field to reference the parent.

So if you don't want Hibernate to load the child when you load the parent, then don't do anything with the child set at all! Just leave it alone! Hands off! You don't even need to override setChild() at all.

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 4:41 pm 
Beginner
Beginner

Joined: Thu Aug 28, 2003 3:09 pm
Posts: 39
Location: Brazil
Hi RobJellinghaus,

I'm doing the modification in the setter of my parent entity as per the parent-child document says to easy the mantainence of the relationship when adding new childs or setting a childs collection.

So, instead of having to remember to set the parent in child when adding a new element, or in this case when setting the child collection, I just set it.

But I'm thinking about adding the method addChild as said in the parentchild doc and remove the parent setting from the setters so I don't get in troubles like that. :)

Thanks for commenting.


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