oracle Hibernate Criteria Query Join with Null Value

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4715627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:28:43  来源:igfitidea点击:

Hibernate Criteria Query Join with Null Value

javaoraclehibernatecriteria-api

提问by nwwatson

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.

我有一个叫做角色的表。每个角色可能属于一个组织。不属于组织的角色的值为 null。我想找到特定组织的所有角色,或者该组织在表中为空。

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?

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

回答by thomaux

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));

回答by Jim

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".

这似乎表明您在某处使用“组织”,大概应该是“组织”。