ITeye博客

Hibernate Example.setPropertySelector

side91 · 2009-08-24 10:33 · hibernate3 · Markdown

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()

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。