1

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?

1
  • 1
    does com.myname.models.Organiation.getId() exist, and are you throwing an exception in it? Commented Jan 17, 2011 at 17:12

2 Answers 2

3

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));
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Nope, had to retype the code, so the error was in the translation between computers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.