限制 Hibernate Criteria 查询中关联的类

发布于 2024-09-06 01:01:23 字数 152 浏览 1 评论 0原文

我有一个对象“Response”,它有一个名为“submitter”的属性。提交者可以是几个不同类别之一,包括学生、教师等...

我希望能够执行条件查询,其中我仅选择教师提交的响应 - 因此这意味着对关联实体施加类别限制。对此有什么想法吗?我想远离直接 SQL 或 HQL。

I have an object, 'Response' that has a property called 'submitter'. Submitters can be one of several different classes including Student, Teacher, etc...

I would like to be able to execute a criteria query where I select only responses submitted by teachers - so this means putting a class restriction on an associated entity. Any ideas on this? I'd like to stay away from direct SQL or HQL.

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

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

发布评论

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

评论(2

转身泪倾城 2024-09-13 01:01:23

根据 Gavin King 的说法,以下内容适用于 HQL:

where foo.class in (...)

您尝试过吗

c.createCriteria("submitter").add(Restrictions.eq("class", Teacher.class))

According to Gavin King, the following works with HQL:

where foo.class in (...)

Have you tried

c.createCriteria("submitter").add(Restrictions.eq("class", Teacher.class))

?

落花浅忆 2024-09-13 01:01:23

我最终使用了原生 SQL。不是最好的,但也并不可怕:

Criteria c = session.createCriteria(Response.class);
c.createCriteria("submitter").add(Restrictions.sqlRestriction("{alias}.person_type='student'"));

值得注意的是,这仅在我使用每个继承层次结构表映射策略时才有效。没有它,我就不会有鉴别器专栏。

I ended up using native SQL. Not the greatest, but it isn't horrible either:

Criteria c = session.createCriteria(Response.class);
c.createCriteria("submitter").add(Restrictions.sqlRestriction("{alias}.person_type='student'"));

It's worth noting that this only worked because I was using the table-per-inheritance-hierarchy mapping strategy. Without that, I wouldn't have had a discriminator column.

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