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: Loading collection without loading the parent object
PostPosted: Mon Feb 08, 2010 6:47 pm 
Newbie

Joined: Mon Feb 08, 2010 6:23 pm
Posts: 2
Hibernate does a nice job lazy loading data. However, there is one scenario I can't quite figure out how to make work without use of queries.
Suppose a have a PARENT object that has a collection of CHILDREN (the scenario is purely artificial just to explain the idea). Suppose PK on the PARENT is just a single field type Long. CHILDREN refers PARENT through a foreign key that points to that PK.

So (if I don't use queries) naturally I start from loading parent

Parent parent = (Parent)session.load(Parent.class, <SOMEID>);

At this point the actual PARENT row is not loaded yet (which is great). All I have is a proxy
Now if I try to touch the collection of children:

for (Child child: parent.getChildren())
{
...
//Do something with child... "parent" is not touched
...
}

I can see that first the PARENT row is loaded and then a separate SQL is executed to load the list of children:

SELECT * FROM PARENT WHERE ID = <SOMEID>
SELECT * FROM CHILDREN WHERE PARENT_ID = <SOMEID>

This is kind of inefficient. There is no need for the first select statement as the PK is already known (<SOMEID>)

Id on Parent is marked with @Id annotation and getChildren collection is annotated with @OneToMany(mappedBy="parent")

Do I miss some annotation to avoid the first statement? Is this the way Hibernate works? Does it make sense to submit some suggestion for improving the area in one of the future releases?

Thanks in advance...


Top
 Profile  
 
 Post subject: Re: Loading collection without loading the parent object
PostPosted: Wed Feb 10, 2010 4:15 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Is this the way Hibernate works?


I guess Yes, and I have also an explanation for this:
-usually when you get a persistent collection i.e. by calling parent.getChilds()
then the query (SELECT * FROM CHILDREN WHERE PARENT_ID = <SOMEID> ) is executed
and the result is stored in the persistent collection.
-further calls of parent.getChilds() within the same persistent context will not execute the query again, as the persistent collection is already filled (initialized) and this is good.
-the persistent collection representing the parent-child relationship cannot be referred by the proxy-object.
-only the real implementation (the de-proxied instance) can keep a reference the persistent collection
-for this reason I guess, hibernate has to get the real implementationof the proxy by calling
SELECT * FROM PARENT WHERE ID = <SOMEID>


Top
 Profile  
 
 Post subject: Re: Loading collection without loading the parent object
PostPosted: Wed Feb 10, 2010 8:22 am 
Newbie

Joined: Mon Feb 08, 2010 6:23 pm
Posts: 2
Thanks for your reply! I didn't look into much inside of proxy objects implementation (which I know sources are available), but if I were to approach implementing it, I'd make it an instance of the real class with a flag "not loaded yet". Once I touch anything that require object loaded, I'd reset the flag and execute SELECT. Not sure if Hibernate proxy works this way thou. I suspect it is, as, for instance, getting Id column from a proxy object does not result in the real object being loaded (SELECT is not executed until some non-id column or collection is touched).

Anyways, hopefully Hibernate devs scan forums periodically. May be some food for thought for them... It doesn't matter what the internal implementation is, the parent query in the case is just something that not need to be there.


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.