Home > Java > javaTutorial > How to solve the problem of Chinese garbled error when Hibernate inserts data

How to solve the problem of Chinese garbled error when Hibernate inserts data

不言
Release: 2018-10-11 15:06:27
forward
2320 people have browsed it

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>
Copy after login

Check the corresponding source code and you can see that there is a method, as follows:

@Override  
public String getTableTypeString()
{  
  return "ENGINE=InnoDB";  
}
Copy after login

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";    
    }  
}
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template