MyPropertySelector
package util;
import org.hibernate.criterion.Example.PropertySelector;
import org.hibernate.type.Type;
@SuppressWarnings("serial")
public class MyPropertySelector {
private static PropertySelector NOT_NULL_BLANK_ZEROS = null;
private MyPropertySelector(){
}
public static PropertySelector getNotNullBlankZeros() {
if(NOT_NULL_BLANK_ZEROS == null){
NOT_NULL_BLANK_ZEROS = new PropertySelector() {
public boolean include(Object value, String propertyName, Type type) {
return value != null
&& (((value instanceof String) && !("").equals(value))
|| ((value instanceof Number) && ((Number) value).longValue() != 0));
}
};
}
return NOT_NULL_BLANK_ZEROS;
}
}
Criteria c = session.createCriteria(Student.class);
l = c.add(Example.create(s)
.setPropertySelector(
MyPropertySelector.getNotNullBlankZeros())
.enableLike()
).list()
本文介绍了一个自定义的Hibernate PropertySelector实现——MyPropertySelector,用于在查询过程中排除空值、空白字符串及数值零值的情况。通过具体代码展示了如何创建并应用这个筛选器到Hibernate的查询条件中。

被折叠的 条评论
为什么被折叠?



