Hibernate uses HQL to do Chinese sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.