Home >Java >javaTutorial >How to solve the problem of Chinese garbled error when Hibernate inserts data
The content of this article is about the solution to the Chinese garbled error message when Hibernate inserts data. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Error description
The program is run and data (including Chinese) is inserted into the table and an error is reported: \xE6\xB2 \x88\xE9\x9B\xAA...
##But when I create a new database and manually insert data into Chinese, it works fine. I also modify the database and it doesn’t work after encoding the table. Moreover, this situation will also occur under MySQL5.7 and cannot be solved.
Problem Solution
In the Hibernate main configuration file, we will configure the database dialect , the general configuration is as follows:<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>Check the corresponding source code and you can see that there is a method, as follows:
@Override public String getTableTypeString() { return "ENGINE=InnoDB"; }So we can customize a class and rewrite the above method as follows:
package com.taohan.util; import org.hibernate.dialect.MySQL5InnoDBDialect; public class HibernateEncodeAdapter MySQL5InnoDBDialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8"; } }Finally modify the Hibernate main configuration file and specify the database dialect The class is the one we customized, as follows:
<property name="dialect">com.taoahn.util.HibernateEncodeAdapter</property>Of course, it is best to add ## after setting the database connection path in the Hibernate main configuration file.
#?useUnicode=true&characterEncoding=UTF-8, as follows:
<property name="hibernate.connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property>
The above is the detailed content of How to solve the problem of Chinese garbled error when Hibernate inserts data. For more information, please follow other related articles on the PHP Chinese website!