开发者

Hibernate Criteria Query Join with Null Value

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.Or开发者_Go百科ganization" 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜