现在的位置: 首页 > 综合 > 正文

Hibernate 实现Oracle的中文汉字按照拼音排序 之延续,通用的查询排序能力。

2013年02月20日 ⁄ 综合 ⁄ 共 1343字 ⁄ 字号 评论关闭

之前有写过oracle 结合hibernate支持拼音排序,优化一下,之前写的有一个小的bug,就是在重写了hibernate 的Order接口后,出现所有的字段都通过拼音排序,

导致时间、数字类型的排序都乱了,加一个判断,之正对string类型做拼音排序

原文地址:http://blog.csdn.net/dracotianlong/article/details/8637561

修改后的代码如下

package com.sencloud.dh.core.command;

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

/**
 * 实现Oracle数据库中文字段按照拼音排序问题 <一句话功能简述> <功能详细描述>
 * 
 * @author xutianlong
 * @version [版本号, Mar 5, 2013]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class OracleOrder extends Order
{
    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);
        Type propertyType= criteriaQuery.getTypeUsingProjection(criteria, propertyName);
//只有string类型支持拼音排序
        if("string".equals(propertyType.getName()))
        {
        	  return " nlssort(" + columns[0] + ",'NLS_SORT=SCHINESE_PINYIN_M') " + (ascending ? "" : "desc");
        }
        else
        {
        	return super.toSqlString(criteria, criteriaQuery);
        }
     
      
    }

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

}

抱歉!评论已关闭.