Hibernate / JPA import.sql utf8字符损坏 [英] Hibernate/JPA import.sql utf8 characters corrupted

查看:91
本文介绍了Hibernate / JPA import.sql utf8字符损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用import.sql将我的开发数据写入DB。我使用MySQL Server 5.5和我的persistence.xml在这里:

 <?xml version =1.0encoding = UTF-8?> 
< persistence version =2.0
xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/ 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd >
< persistence-unit name =MobilHMtransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> tr.com.stigma.db.entity.Doctor< / class>
< class> tr.com.stigma.db.entity.Patient< / class>
< class> tr.com.stigma.db.entity.Record< / class>
< class> tr.com.stigma.db.entity.User< / class>
< properties>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
<! - 自动检测注释模型类 - >
< property name =hibernate.archive.autodetectionvalue =class/>
<! - 数据源 - >
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.usernamevalue =mobilhm/>
< attribute name =hibernate.connection.passwordvalue =mobilhm/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost / mobilhm/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< / properties>
< / persistence-unit>





sql在DB中未正确显示。例如字符ü在db中变成¼。 mysql中的默认字符集是utf-8,我正在创建如

的表。

  CREATE TABLE doctor(doctorId int unsigned NOT NULL AUTO_INCREMENT, name varchar(45)NOT NULL,surname varchar(45)NOT NULL,PRIMARY KEY(doctorId))ENGINE = InnoDB DEFAULT CHARSET = utf8; 

很奇怪,如果我使用Mysql导入导入/导出管理器数据是正确的,但使用hibernate.hbm2ddl



如何解决这个问题?
我已尝试添加

 < property name =hibernate.connection.useUnicodevalue =true/> 
< property name =hibernate.connection.characterEncoding
value =UTF-8/>
< property name =hibernate.connection.charSetvalue =UTF-8/>

到persistence.xml。但它没有帮助。



修复:
我最终解决了。我使用Tomcat,这是腐败的点不是hibernate或mysql。我已经开始使用set JAVA_OPTS = -Dfile.encoding = UTF-8命令,我的问题消失了。



问题的标题现在变得误导。抱歉,

解决方案

当创建该文件的阅读器时,Hibernate使用 new InputStreamReader(stream) ; 直接,不显式编码(默认执行平台charset编码是假定/使用)。



您的 import.sql 文件必须位于默认的执行平台字符集编码中。



这是一个旧的(2006年!)开放的问题,以防万一希望发送补丁: https: //hibernate.onjira.com/browse/HBX-711


I'm using import.sql to write my development data to DB. I'm using MySQL Server 5.5 and my persistence.xml is here:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MobilHM" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>tr.com.stigma.db.entity.Doctor</class>
    <class>tr.com.stigma.db.entity.Patient</class>
    <class>tr.com.stigma.db.entity.Record</class>
    <class>tr.com.stigma.db.entity.User</class>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <!-- Auto detect annotation model classes -->
        <property name="hibernate.archive.autodetection" value="class" />
        <!-- Datasource -->
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="mobilhm" />
        <property name="hibernate.connection.password" value="mobilhm" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost/mobilhm" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>

Some characters in my import.sql is not shown correctly in DB. For example character ü becomes ü in db. Default charset in mysql is utf-8 and I'm creating tables like

CREATE TABLE doctor (doctorId int unsigned NOT NULL AUTO_INCREMENT, name varchar(45) NOT NULL, surname varchar(45) NOT NULL, PRIMARY KEY (doctorId)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

It is weird that if I import using Mysql import/export manager data is correct but using hibernate.hbm2ddl.auto=create makes characters corrupted.

How can I solve this?

Edit: Also I've tried adding

<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding"
            value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />

to persistence.xml. But it didn't help.

Fix: I've solved it eventually. I'm using Tomcat and that is the point of corruption not hibernate or mysql. I've started it with set JAVA_OPTS=-Dfile.encoding=UTF-8 command and my problem goes away.

The title of question became misleading now. Sorry for that.

解决方案

When creating the reader for that file, Hibernate uses new InputStreamReader(stream); directly, without explicit encoding (the default execution platform charset encoding is assumed/used).

So, in other words, your import.sql file must be in the default execution platform charset encoding.

There is an old (2006!) open issue for this, in case one wishes to send a patch: https://hibernate.onjira.com/browse/HBX-711

这篇关于Hibernate / JPA import.sql utf8字符损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆