Hibernate - 列表中类的限制

发布于 2024-09-04 21:48:34 字数 765 浏览 0 评论 0原文

我正在尝试拉回集合中具有特定类型项目的项目列表。

例如:

<class name="Owner" table="OWNER">
<id name="id" column="OWNER_ID" />
<set name="cats" table="OWNER_CATS" lazy="false">
    <key column="OWNER_ID" />
    <many-to-many class="Cat" />
</set>

<class name="Cat" table="CAT" discriminator-value="C">
<id name="id" column="CAT_ID" />
<discriminator column="type" type="character" />

<subclass name="Lion" discriminator-value="L">
    <property name="teeth" />
</subclass>
</class>

使用限制,我如何获得饲养狮子作为宠物的主人名单?

我尝试了以下方法但无济于事:

criteria.createCriteria("cats").add(Restrictions.eq("class", Lion.class));

I'm trying to pull back a list of items that have a specific type of item in a set.

For example:

<class name="Owner" table="OWNER">
<id name="id" column="OWNER_ID" />
<set name="cats" table="OWNER_CATS" lazy="false">
    <key column="OWNER_ID" />
    <many-to-many class="Cat" />
</set>

<class name="Cat" table="CAT" discriminator-value="C">
<id name="id" column="CAT_ID" />
<discriminator column="type" type="character" />

<subclass name="Lion" discriminator-value="L">
    <property name="teeth" />
</subclass>
</class>

Using restrictions how would I get a list of owners who have lions as pets?

I've tried something along the lines of the following to no avail:

criteria.createCriteria("cats").add(Restrictions.eq("class", Lion.class));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

护你周全 2024-09-11 21:48:34

目前(尽管我不确定这是否是故意的),其工作方式是您必须将鉴别器值指定为限制值,而不是类本身,所以:

criteria.createCriteria("cats").add(Restrictions.eq("class", "L"));

在您的示例中。

At the moment (though I'm not sure if this is intentional) the way this works is you have to specify the discriminator value as the restriction value, rather than the class itself, so:

criteria.createCriteria("cats").add(Restrictions.eq("class", "L"));

in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文