5

I have to create create Criteria or Criterion on specific field myProperity (on class MyClass). I have to choose all objects that have prop = null or satisfy specific Criteria. So I should make something like:

Criteria criteria = this.sessionManager.getCurrentSession().createCriteria(MyClass.class);

specificCriteria = criteria.createCriteria('myProperity');
/* definition of specificCriteria */

Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.isNull('myProperity'));
disjunction.add(specificCriteria);

criteria.add(disjunction);

The problem is caused be the facts:

  1. I can't add Criteria to Disjunction (to Disjunction can be added only a Criterion), so line: disjunction.add(specificCriteria); is wrong
  2. I can't somehow modify the specificCriteria, to accept null because I can't make criteria on null. (It gives me NullPointerException)

Have you any Idea how to deal with it?

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
M314
  • 925
  • 3
  • 13
  • 37

5 Answers5

10

You can get answer of all question here this will help you

for ex if your MyClass like

class MyClass{
   private long id;
   private String myProperity;
   .
   .
   .
   setter & getter
}

here you null issue is solved and this'll oring with other criterias.

Criteria criteria = session.createCriteria(MyClass.class);

Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.isNull("myProperity"));

criteria.add(disjunction);

criteria.add(Restrictions.eq("id", value));
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
7

Just for anyone else with this question. I had a similar issue where I wanted to check if an associated collection was empty and if not restrict by ID. To achieve this, the following helped me (updated code for newer version of hibernate):

Criteria criteria = getSession().createCriteria(StaffMember.class);
criteria.createAlias("franchises", "franchises", JoinType.LEFT_OUTER_JOIN)
        .add(Restrictions.or(
                Restrictions.isEmpty("franchises"),
                Restrictions.in("franchises.id", allowedFranchises)));

So this allows the following:

  1. staff members with no franchises
  2. staff members who are in a franchise with an ID from allowedFranchises collection
DanAngelus
  • 71
  • 1
  • 2
6

Something like this helped me:

final Criteria c = session.createCriteria(House.class)
    // Houses may not have doors.
    .createAlias("door", "door", CriteriaSpecification.LEFT_JOIN)
    // Door may be null, but if it isn't it must be blue.
    .add(Restrictions.or(Restrictions.isNull("door"), Restrictions.eq("door.color", "blue")));

This answer helped me: https://stackoverflow.com/a/2208458

Community
  • 1
  • 1
David
  • 1,157
  • 2
  • 12
  • 19
  • This is the answer to use for Hibernate 3, CriteriaSpecification exists where as JoinType does not. – Zoidberg Mar 05 '19 at 18:34
3

Check if value provided in criteria is null by check isNull and apply to your criteria as following

criteria.add(Restrictions.isNull("parentId"));
taras
  • 6,566
  • 10
  • 39
  • 50
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19
0

Restrictions.eqOrIsNull(propertyName, value) method can be used to handle null values.

Hitesh Gupta
  • 1,091
  • 9
  • 14