Hibernate uses hql for Chinese sorting,
Why does MySQL not support Chinese sorting? No way. You only need to wait for utf8_unicode_cn to come out. If you want to implement cross-database with hibernate and do not want to change the code, I will tell you the answer below.
Method 1. Description: register a sorting method for the MySQL dialect package of Hibernate. MySQL Chinese sorting is implemented in this method. Code:
public class MySQLExtendDialect extends MySQLDialect { public MySQLExtendDialect(){ super(); registerFunction("convert_gbk", new SQLFunctionTemplate(Hibernate.STRING, "convert(?1 using gbk)") ); } }
The disadvantage of this method is that it is necessary to change the dialect packages of different data to implement cross-database without code change. (You Need To change order by field to order by convert_gbk (field )).
Method 2. Description: Modify the MySQL dialect package of Hibernate separately and modify the order by method so that the convert method is used by default to sort Chinese characters. Code:
public class MySQLGBKDialect extends MySQLDialect{ public MySQLGBKDialect() { } public String renderOrderByElement(String expression, String collation, String order, NullPrecedence nulls) { expression = (new StringBuilder("convert(")).append(expression).append(" using 'gbk')").toString(); return super.renderOrderByElement(expression, collation, order, nulls); }}
RenderOrderByElement this method is used to rewrite the methods in the parent class. The advantage is that you do not need to modify other data dialect packages or code. The disadvantage is that all sorting methods will execute the convert method.