将子级与父级连接起来的条件 api 等效项
版本:Hibernate 3.3
你好, 我有 2 个简单的模型:
class Parent {
Long id; //auto generated sequence and primary key
String name;
Set<Child> children;
}
class Child {
Long id;
String name;
Parent parent;
}
使用以下 hbm:
<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>
我想要实现的是:“选择父母姓名为‘John’的所有孩子。我无法弄清楚如何为看起来像这样的 hql 编写 Criteria api 等效项这个:
SELECT child
FROM Child as child join child.parent
where parent.name = 'John'
我尝试了下面的一个,但它没有生成预期的连接查询:
Criteria c = session.createCriteria(Child.class);
c.createCriteria("parent").add(Restrictions.eq("name", "John");
c.list();
任何人都可以看到我做错了什么吗?
任何想法将不胜感激。
Version: Hibernate 3.3
Hi,
I have 2 simple models:
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:
<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:
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:
Criteria c = session.createCriteria(Child.class);
c.createCriteria("parent").add(Restrictions.eq("name", "John");
c.list();
Could anybody see what am I doing wrong?
Any ideas would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 JPA 2.0 criteria API,您可以执行以下操作:
Using JPA 2.0 criteria API, you could do :