-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria api equivalent for joining child with parent
PostPosted: Mon Jun 27, 2011 3:32 pm 
Newbie

Joined: Mon Jun 27, 2011 3:25 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.