Hibernate Version: 3.3
Oracle: 11g
Hi, I have 2 simple models:
Code:
class Parent {
Long id; //auto generated sequence and primary key
String name;
Set<Child> children;
}
class Child {
Long id;
String name;
Parent parent;
}
with the following hbm:
Code:
<class name="my.Parent" table=PARENT">
<id name="id" column="PARENT_ID" type="java.lang.Long">
<property name="name" column="NAME" type="java.lang.String">
<set name="children" table="CHILDREN" inverse="true">
<key><column name="PARENT_ID" not-null="true" /></key>
<one-to-many class="my.Child" />
</set>
</class>
<class name="my.Child" table=CHILD">
<composite-id>
<key-many-to-one name="parent" column="PARENT_ID" class="my.Parent" />
<key-property name="id" column="CHILD_ID" type="java.lang.Long" />
</composite-id>
<property name="name" column="NAME" type="java.lang.String">
</class>
What I want to achieve is this: "select all children whose parent's name is 'John'. I am not able to figure out how to write Criteria api equivalent for a hql that looks like this:
Code:
SELECT child
FROM Child as child join child.parent
where parent.name = 'John'
I tried the below one but its not generating the expected join query:
Code:
Criteria c = session.createCriteria(Child.class);
c.createCriteria("parent").add(Restrictions.eq("name", "John");
c.list();
HQL query results in a join between child and parent on parent_id as expected. But Criteria API is just adding parent criteria restrictions to the main criteria but neither adding parent table in from clause nor adding where condition for join on parent_id.
Any suggestions on what am I doing wrong and how to correct it would be greatly appreciated.
Thanks.
Bhargava