Hibernate Criteria 查询连接与空值

发布于 2024-10-12 14:03:29 字数 735 浏览 3 评论 0原文

我有一个名为角色的表。每个角色可能属于一个组织。不属于组织的角色的值为 null。我想查找特定组织的所有角色或该组织在表中为空的位置。

Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Role.class)
.add(Restrictions.or(
    Restrictions.eq("organization",organization.getId()), 
        Restrictions.isNull("organization")));

映射文件具有:

<many-to-one class="com.myname.models.Organization" name="organization">
<column name="organization_id" precision="22" scale="0" />
</many-to-one>

当查询运行时,我得到:

IllegalArgumentException occurred calling getter com.myname.models.Organization.id

我发现如果我修改条件以仅查询组织上的空值,一切都会正常,但是一旦我查询一个值,我就会收到错误。

如何修改查询或映射文件以满足我的目标?

I have a table called roles. Each role may belong to an organization. Roles that do not belong to an organization have a value of null. I want to find all the roles for a specific organization or where the organization is null within the table.

Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Role.class)
.add(Restrictions.or(
    Restrictions.eq("organization",organization.getId()), 
        Restrictions.isNull("organization")));

The Mapping file has:

<many-to-one class="com.myname.models.Organization" name="organization">
<column name="organization_id" precision="22" scale="0" />
</many-to-one>

When the query runs, I get:

IllegalArgumentException occurred calling getter com.myname.models.Organization.id

I have found that if I modify the criteria to just query for nulls on organization everything works, however once I query for a value, I get the error.

How to I modify the query or mapping file to meet my goals?

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

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

发布评论

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

评论(2

尴尬癌患者 2024-10-19 14:03:29

不确定您是否仍在寻找答案,但由于我在寻找同一问题的解决方案期间遇到了此线程,我认为它可能对将来的参考有所帮助。

您需要按如下方式构建您的标准:

final Criterion lhs = Restrictions.eq("organization",organization.getId());
final Criterion rhs = Restrictions.isNull("organization");
Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Role.class).add(Restrictions.or(lhs, rhs));

Not sure whether you're still looking for the answer, but as I've encountered this thread during my search for a solution of the same problem, I though it might be helpful for future reference.

You'll need to construct your criteria as follows:

final Criterion lhs = Restrictions.eq("organization",organization.getId());
final Criterion rhs = Restrictions.isNull("organization");
Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Role.class).add(Restrictions.or(lhs, rhs));
遗弃M 2024-10-19 14:03:29
IllegalArgumentException occurred calling getter com.myname.models.Organiation.id

这似乎表明您在某个地方使用了“组织”,而该地方应该是“组织”。

IllegalArgumentException occurred calling getter com.myname.models.Organiation.id

This seems to suggest that you are using "Organiation" somewhere whhere presumably this should be "Organization".

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