hsl*_*luo 8 java mysql hibernate
我这样在MySQL中有两行
+---------+---------+
| foo | bar |
+---------+---------+
| | NULL |
| | |
+---------+---------+
Run Code Online (Sandbox Code Playgroud)
空的是空字符串"".
现在我想得到他们两个.我用Criteria与Restrictions.eqOrIsNull()上两列,但它始终只返回一行.
代码是这样的
criteria.add(Restrictions.eqOrIsNull("foo", ""));
.add(Restrictions.eqOrIsNull("bar", ""));
Run Code Online (Sandbox Code Playgroud)
当我仅添加条件时foo,它返回两行.但是bar,因为它只返回第二个,它是空的.
该javadoc的说,Apply an "equal" constraint to the named property. If the value is null, instead apply "is null".所以我会收到这个错误,还是应该以其他方式使用?
更新:
对不起,我太粗心了.医生明确说明了这一点.此方法根据value传递给它而工作,而不是存储在DB中的命名属性的实际值.Github
上的源代码:
public static Criterion eqOrIsNull(String propertyName, Object value) {
return value == null
? isNull( propertyName )
: eq( propertyName, value );
}
Run Code Online (Sandbox Code Playgroud)
所以在我的情况下,eqOrIsNull返回eq("").我应该使用的脱节eq和isNull,像格雷戈里回答.
检查此代码是否符合您的要求 -
criteria.add(Restrictions.or(Restrictions.eq("foo", ""), Restrictions.isNull("foo")))
.add(Restrictions.or(Restrictions.eq("bar", ""), Restrictions.isNull("bar")));
Run Code Online (Sandbox Code Playgroud)
此代码片段使用 Hibernate 3.5.0-CR-2 API。
| 归档时间: |
|
| 查看次数: |
4451 次 |
| 最近记录: |