I have two rows in MySQL such as
+---------+---------+ | foo | bar | +---------+---------+ | | NULL | | | | +---------+---------+
Where the empty lines are "" .
Now I want to get both of them. I use Criteria and Restrictions.eqOrIsNull() for both columns, but always return 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 returns only the second, which is empty.
javadoc says Apply an "equal" constraint to the named property. If the value is null, instead apply "is null". Apply an "equal" constraint to the named property. If the value is null, instead apply "is null". So am I mistaken, or should it be used in other ways?
UPDATE :
Sorry, I'm so sloppy. Doc makes that clear. This method works in accordance with the value symbol passed to it, and not the actual value of the named property stored in the database.
Download source package 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 had to use the eq and isNull , as Gregory answered.
java mysql hibernate
hsluo
source share