我有两个排在MySQL这样如何使用Hibernate eqOrIsNull()
+---------+---------+
| foo | bar |
+---------+---------+
| | NULL |
| | |
+---------+---------+
当空是空字符串""
。
现在我想要得到他们两个。我在两列上都使用Criteria
和Restrictions.eqOrIsNull()
,但它总是只返回一行。
的代码是这样的
criteria.add(Restrictions.eqOrIsNull("foo", ""));
.add(Restrictions.eqOrIsNull("bar", ""));
当我只在foo
添加条件,它返回两行。但是对于bar
,它只返回空的第二个。
javadoc说,Apply an "equal" constraint to the named property. If the value is null, instead apply "is null".
所以我得到这个错误,或者应该以其他方式使用?
UPDATE:
对不起,我这么粗心。该文件明确指出。此方法根据传递给它的value
工作,而不是存储在DB中的命名属性的实际值。在Github
的源代码:
public static Criterion eqOrIsNull(String propertyName, Object value) {
return value == null
? isNull(propertyName)
: eq(propertyName, value);
}
在我的情况
所以,eqOrIsNull
回报eq("")
。我应该使用eq
和isNull
的分离,如Gregory answered。
对于这类问题,您应该启用调试日志记录。它会让hibernate打印正在执行的sql。 – adam0404
就像'select foo,bar from table where foo =?和bar =?' – hsluo