Hibernate uses hql for Chinese sorting,

Source: Internet
Author: User

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.




Related Article

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.