DetachedCriteria mysql 中文排序

使用hibernate的detachedCriteria进行查询,需要对字符串排序,按照hibernate提供的排序方式并不是十分爽,实现按照拼音排序,数据库使用的mysql。

MysqlOrder .java 写道
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Order;

public class MysqlOrder extends Order{
private String propertyName;

protected MysqlOrder(String propertyName) {
super(propertyName, false);
this.propertyName = propertyName;
}

/**
* 只考虑按一个字段排序的情况
*/
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
return " CONVERT( "+ columns[0] +" USING GBK) ";
}

public static MysqlOrder getOrder(String propertyName)
{
return new MysqlOrder(propertyName);
}
}

 

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