Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Criteria on field that can be null?

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?

like image 574
M314 Avatar asked Mar 28 '12 08:03

M314


1 Answers

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));
like image 122
Yogesh Prajapati Avatar answered Oct 10 '22 03:10

Yogesh Prajapati