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&characterencoding=utf-8
Hibernate build table defaults to UTF-8 encoding