With Hibernate+mysql's children's shoes is not very vexed why MySQL does not support Chinese sorting it? No way, only wait for the UTF8_UNICODE_CN out. If you want to use Hibernate to achieve cross-library, but also want to change the code how to implement it, below to tell you the answer.
Method One, Description: to hibernate the MySQL dialect packet registration A sorting method, the method implements the MySQL Chinese sorting. Post 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 we need to change the dialect packet of different data to realize the cross-Library without changing the code. (You need to change the order by field to order by CONVERT_GBK (field)).
Method Two, Description: Modify Hibernate's MySQL dialect package separately, modify the order by method, so that the method by default using the Convert method to achieve Chinese sorting. Post 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 The method is a method in the replication parent class, the advantage is not to change the other data dialect package, without changing the code, the disadvantage is that all sorting methods will be executed by the Convert method.
Hibernate uses HQL to do Chinese sort