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>
@Override public String getTableTypeString() { return "ENGINE=InnoDB"; }
package com.taohan.util; import org.hibernate.dialect.MySQL5InnoDBDialect; public class HibernateEncodeAdapter MySQL5InnoDBDialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8"; } }
<property name="dialect">com.taoahn.util.HibernateEncodeAdapter</property>
#?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!