将子级与父级连接起来的条件 api 等效项

发布于 2024-11-17 01:29:47 字数 1438 浏览 3 评论 0原文

版本: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-11-24 01:29:47

使用 JPA 2.0 criteria API,您可以执行以下操作:

criteriaBuilder cb =...
CriteriaQuery<child> q=....
Root<child> entity= ..... 
Join<child, parent> o = entity.join(child_.parent);
Path<parent> c = entity.get(child_.parent);
Path<String> name = c.get(parent_.name);
....  where( cb.equal(name, "john"));

Using JPA 2.0 criteria API, you could do :

criteriaBuilder cb =...
CriteriaQuery<child> q=....
Root<child> entity= ..... 
Join<child, parent> o = entity.join(child_.parent);
Path<parent> c = entity.get(child_.parent);
Path<String> name = c.get(parent_.name);
....  where( cb.equal(name, "john"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文