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
|