Hibernate 自定义Order

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Order;

/**
 * Oracle数据库中文排序
 *   
 */
public class OracleOrder extends Order {

	private static final long serialVersionUID = -7866385945784052610L;

	private String propertyName;
	private boolean ascending;

	protected OracleOrder(String propertyName, boolean ascending) {
		super(propertyName, ascending);
		this.propertyName = propertyName;
		this.ascending = ascending;
	}

	/**
	 * 只考虑按一个字段排序的情况
	 */
	public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
			throws HibernateException {
		String[] columns = criteriaQuery.getColumnsUsingProjection(criteria,
				propertyName);
		return " nlssort(" + columns[0] + ",'NLS_SORT=SCHINESE_PINYIN_M') "
				+ (ascending ? "" : "desc");
	}

public static OracleOrder getOrder(String propertyName, boolean ascending) {
		return new OracleOrder(propertyName, ascending);
	}
}





 DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.addOrder(OracleOrder.getOrder("username", true));
getHibernateTemplate().findByCriteria(dc, startRowIndex,maxCountPerset);


转自: http://www.oschina.net/code/snippet_176115_17508


public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { 
		String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);  
		return " decode("+columns[0]+",'',0,to_number("+columns[0]+")) ";  
	}  

你可能感兴趣的:(Hibernate)