-->
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: Need a definitive answer on loading a collection subset
PostPosted: Fri Jul 14, 2006 3:03 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

I've been around this block a couple hundred times now, and I still can't make it work. Maybe it's not possible with Hibernate - could someone answer this one for me?

Here's the db model (tables):
PARENT - CHILD - GRAND_CHILD

Here's the data:

PARENT: parent_id=0

CHILD: child_id=0, parent_id=0
CHILD: child_id=1, parent_id=0

GRAND_CHILD: grand_child_id=0, child_id=0 <<< looking for this one
GRAND_CHILD: grand_child_id=1, child_id=1

Those are my Hibernate classes:
Parent, Child, GrandChild

Parent has a collecion of Child objects (children; one-to-many), and Child has a collection of GrandChild objects (grandChildren; one-to-many).

My query is this:

Criteria c=session.createCriteria(Parent.class).createCriteria("children").createCriteria("grandChildren");
c.add(Expression.eq("grandChildId",0));

Here's my problem: this query always returns single Parent object, but the children collection of the Parent class contains both Child records (with their respective grandChildren records).

Is there a way to tell Hibernate not to load the Child element that does not match the query? From what I can tell the query is applied to top-level table only (Parent), and then everything below that is loaded without any consideration for placed criteria.

So I'd like to know if this can be done - loading only the records (and the children) that match the criteria (so only one Child would be in the collection, not two).
Also, I'd like to know if this can be done WITHOUT the use of Hibernate filters.
I'd greatly appreciate it if someone experienced with Hibernate could answer this for me, one way or another.
Thanks,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 4:47 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I don't think so. Criteria makes the sql query in order, so it will always go

parent join child (both children that match returned) then it does
child join grandchildren (grandchildren that match returned)
grandchild that has id=0 saved, others discarded

All the stuff is loaded anyways to compare, so it returns it.

There is a unique result method for Criteria, but I'm not sure how well it works in 3 tiered relationships

Best way I know to have this functionality is in hql, or a sql query to get the identifiers then running a standard hibernate load command.

Also, please refrain from posting the same thread 4 times.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 11:53 am 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

Thank you for your reply. I don't quite understand what are you saying, though. Unique result method that you talk about does not really help with anything below the top level object. Same for result transformer DISTINCT_ROOT_ENTITY - both apply to top level only, and the children of the top level object are not affected.

Same for using sql or hql to load identifiers - in this particular case a single id will be returned (parent id of 0) and then if I use load on that id I will get a full hierarchy, including child and grandchild records I didn't ask for.

The closest thing I got to was using this HQL:

"from Parent as p join p.children as c join c.grandChildren as gc where gc.grandChildId=0"

That HQL statement returned an array of objects, containing parent, child and grand child records leading to grandChildId=0 (so only one set of data was returned, as opposed to two sets of data returned by any other query).

If I could get those same results packaged into a Parent object, I would be happy. Unfortunately, I can't get it to work.
Any thoughts on this one?

PS. As for multiple posts, Hibernate Forum engine was failing with an error related to PHP something, so I didn't know if my question got posted or not. So I kept pressing the Submit button.


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.