Hibernate build table defaults to UTF-8 encoding

Source: Internet
Author: User

First, the question:

Hibernate automatic table encoding should be the default encoding format of the data, generally not utf-8. So want to build a table default encoding is UTF-8, what should be done?

Second, the solution:

Take MySQL for example:

(i), modify the dialect of Hibernate-built table

1, the general situation we use the MySQL dialect for: org.hibernate.dialect.MySQL5Dialect

The default return is

@Override        Public String gettabletypestring () {          return "Engine=innodb";      }  

2. Overriding the Mysql5innodbdialect class, overriding the Gettabletypestring method

 Package com.lqy.spring.hibernate.mysql;     Import org.hibernate.dialect.MySQL5InnoDBDialect;      Public class extends mysql5innodbdialect{        @Override      public  String gettabletypestring () {           return "Engine=innodb DEFAULT Charset=utf8";        }  }  

3. Dialect configuration use our rewritten class, configured as follows:

(1) JPA Database Connection Configuration:

Put the default configuration

<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.MySQL5Dialect"/>  

Modified into

<property name= "Hibernate.dialect" value= "Com.lqy.spring.hibernate.mysql.MySQL5DialectUTF8"/>  

(2) Spring Integrated hibernate configuration:

<prop key= "Hibernate.dialect" >com.lqy.spring.hibernate.mysql.MySQL5DialectUTF8</prop>  
<property name= "Jpaproperties" >              <props>                                                  <!--auto-build table type Validate|create|create-drop|update-- >                  <prop key= "Hibernate.hbm2ddl.auto" >update</prop>                  <!--whether to show SQL--                  <prop key= "Hibernate.show_sql" >false</prop>                  <!--shows if SQL is formatted--                  <prop key= " Hibernate.format_sql ">false</prop>                  <prop key=" Hibernate.dialect "> com.lqy.spring.hibernate.mysql.mysql5dialectutf8</prop>              </props>          </property>  

4. Modify the Data connection URL

Jdbc.url=jdbc:mysql://192.168.1.11:3306/db?useunicode=true&amp;characterencoding=utf-8 

Hibernate build table defaults to UTF-8 encoding

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.