I have two rows in MySQL like this
+---------+---------+
| foo | bar |
+---------+---------+
| | NULL |
| | |
+---------+---------+
Where empty are empty strings ""
.
Now I want to get both of them. I use Criteria
and Restrictions.eqOrIsNull()
on both columns, but it always returns only one row.
The code is like this
criteria.add(Restrictions.eqOrIsNull("foo", ""));
.add(Restrictions.eqOrIsNull("bar", ""));
And when I add criteria only on foo
, it returns two rows. But for bar
, it only returns the second, which is empty.
The javadoc says, Apply an "equal" constraint to the named property. If the value is null, instead apply "is null".
So am I getting this wrong, or it should be used in other ways?
UPDATE:
Sorry, I'm so careless. The doc states it clearly. This method works according to the value
passed to it, not the actual value of the named property stored in DB.
Source code on Github:
public static Criterion eqOrIsNull(String propertyName, Object value) {
return value == null
? isNull( propertyName )
: eq( propertyName, value );
}
So in my case, eqOrIsNull
returns eq("")
. I should have used disjunction of eq
and isNull
, like Gregory answered.