hibernate 支持中文排序

public class Oracle_Order extends Order implements Serializable {

	
	/**
	 * 扩展order 支持oracle 按笔划,部首,拼音排序。
	 */
	private static final long serialVersionUID = 1L;
	// SCHINESE_STROKE_M' SCHINESE_RADICAL_M'); 'NLS_SORT=SCHINESE_PINYIN_M
	private static int pinyin = 0; // 'NLS_SORT=SCHINESE_STROKE_M'
	private static int radical = 1; // SCHINESE_RADICAL_M
	private static int stroke = 2; // SCHINESE_STROKE_M
	private String propertyName;
	private int ascending = 0;
	private  int nulls = 0; //如果排序字段为空的话,空排最前。否则最后 //nulls first nulls last
	
	
	protected Oacle_Order(String propertyName, int ascending , int nulls) {
		super(propertyName, false);
		// TODO Auto-generated constructor stub
		this.propertyName = propertyName;
		this.ascending = ascending;
		this.nulls = nulls;
	}
	
	/**
	 * 只对准排序字段只有一个的情况,多个未考虑。
	 */
	public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 
	throws HibernateException {
		String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
		String fragment = new String();
//		nlssort(name,'NLS_SORT=SCHINESE_PINYIN_M') nulls first
		if(this.ascending == 0)
			fragment =" nlssort("+ columns[0] +",'NLS_SORT=SCHINESE_PINYIN_M') nulls "+(this.nulls==0?"first":"last");
		else if(this.ascending == 1)
			fragment =" nlssort("+ columns[0] +",'NLS_SORT=SCHINESE_RADICAL_M') nulls "+(this.nulls==0?"first":"last");
		else
			fragment =" nlssort("+ columns[0] +",'NLS_SORT=SCHINESE_STROKE_M') nulls "+(this.nulls==0?"first":"last");
//		for ( int i=0; i<columns.length; i++ ) {
//			fragment.append( columns[0] ).append( ascending ? " asc" : " desc" );
//			if ( i<columns.length-1 ) fragment.append(", ");
//		}
		return fragment.toString();
	}


	public static Oacle_Order pinyu(String propertyName,int nulls)
	{
		return new Oacle_Order(propertyName,Oacle_Order.pinyin,nulls);
	}
	
	public static Oacle_Order radical(String propertyName,int nulls)
	{
		return new Oacle_Order(propertyName,Oacle_Order.radical,nulls);
	}
	
	public static Oacle_Order stroke(String propertyName,int nulls)
	{
		return new Oacle_Order(propertyName,Oacle_Order.stroke,nulls);
	}

}

你可能感兴趣的:(oracle,Hibernate)