How to use Hibernate eqOrIsNull()
Apply an "equal" constraint to the named property. If the value is null, instead apply "is null".
This means, is null
will be applied only if NULL is passed as the value.
If you specify any other string as value, only equal
will be applied for that.
This is useful when you're not sure about the actual value being passed at runtime as argument to the parameter value. In such a scenario, in a traditional way, either you need to put a not null check & write criteria based on the not null condition or you need to write criteria in the way mentioned by Gregory's answer.
Keeping all these facts in mind, you should get answer of your question. You're getting only those rows which contain empty value & not the one having NULL value, because you have specified an empty string as 2nd arguement. If you specify NULL as the 2nd arguement, you'll get only those rows having NULL value.
Let me know if that's helpful to you.
Check if this code does what you want -
criteria.add(Restrictions.or(Restrictions.eq("foo", ""), Restrictions.isNull("foo")))
.add(Restrictions.or(Restrictions.eq("bar", ""), Restrictions.isNull("bar")));
This snippet uses Hibernate 3.5.0-CR-2 API.