问题 Hibernate / JPA import.sql utf8字符已损坏


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

<?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>

我的import.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.auto = create会导致字符损坏。

我怎么解决这个问题?

编辑: 我也尝试过添加

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

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

固定: 我最终解决了它。我正在使用Tomcat,这是腐败的重点,而不是hibernate或mysql。我用set JAVA_OPTS = -Dfile.encoding = UTF-8命令启动它,我的问题就消失了。

问题的标题现在变得误导了。对不起。


3453
2018-01-23 00:25


起源



答案:


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

所以,换句话说,你的 import.sql 文件必须是默认值 执行 平台charset编码。

有一个旧的(2006年!)开放的问题,如果有人希望发送补丁: https://hibernate.atlassian.net/browse/HBX-711


修复选项:

  • -Dfile.encoding=UTF-8 到了 JAVA_OPTS 环境变量,例如:

    # Linux/Unix
    export JAVA_OPTS=-Dfile.encoding=UTF-8
    # Windows
    set JAVA_OPTS=-Dfile.encoding=UTF-8
    
    # Attention, check before if your JAVA_OPTS doesnt already have a value. If so,
    # then it should be
    export JAVA_OPTS=$JAVA_OPTS -Dfile.encoding=UTF-8
    # or
    set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8
    
  • 在你的Maven插件中设置一个属性(可能是 surefirefailsafe 或者其他,取决于你如何运行导入hibernate文件的代码)。例子 surefire

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <configuration>
          <argLine>-Dfile.encoding=UTF8</argLine>
       </configuration>
    </plugin>
    

10
2018-02-28 18:44



+1出于正确的原因,但假设不正确,因为那时您的测试将取决于平台。最简单的解决方案是将文本文件编码设置为 UTF-8 在您的IDE或 <argLine>-Dfile.encoding=UTF8</argLine> 如果使用maven作为@Jaroslav Frolikov已经说过了。 - G. Demecki
它仍然无法在2017年运作 - gstackoverflow


答案:


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

所以,换句话说,你的 import.sql 文件必须是默认值 执行 平台charset编码。

有一个旧的(2006年!)开放的问题,如果有人希望发送补丁: https://hibernate.atlassian.net/browse/HBX-711


修复选项:

  • -Dfile.encoding=UTF-8 到了 JAVA_OPTS 环境变量,例如:

    # Linux/Unix
    export JAVA_OPTS=-Dfile.encoding=UTF-8
    # Windows
    set JAVA_OPTS=-Dfile.encoding=UTF-8
    
    # Attention, check before if your JAVA_OPTS doesnt already have a value. If so,
    # then it should be
    export JAVA_OPTS=$JAVA_OPTS -Dfile.encoding=UTF-8
    # or
    set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8
    
  • 在你的Maven插件中设置一个属性(可能是 surefirefailsafe 或者其他,取决于你如何运行导入hibernate文件的代码)。例子 surefire

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <configuration>
          <argLine>-Dfile.encoding=UTF8</argLine>
       </configuration>
    </plugin>
    

10
2018-02-28 18:44



+1出于正确的原因,但假设不正确,因为那时您的测试将取决于平台。最简单的解决方案是将文本文件编码设置为 UTF-8 在您的IDE或 <argLine>-Dfile.encoding=UTF8</argLine> 如果使用maven作为@Jaroslav Frolikov已经说过了。 - G. Demecki
它仍然无法在2017年运作 - gstackoverflow


我正在使用import.sql在测试阶段填充数据库,这个链接帮我解决了编码问题: http://javacimrman.blogspot.ru/2011/07/hibernate-importsql-encoding-when.html


3
2018-04-22 09:03





这是一个 可靠的解决方案,无需设置任何系

我们假设导入文件是用。编码的 UTF-8 但是Java默认字符集是不同的,让我们说吧 latin1

1)为。定义自定义类 import_files_sql_extractor hibernate.hbm2ddl.import_files_sql_extractor = com.pragmasphere.hibernate.CustomSqlExtractor

2) 修复无效的字符串 hibernate在实现中读取。

package com.pragmasphere.hibernate;

import org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor;

import java.io.IOError;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class CustomSqlExtractor extends MultipleLinesSqlCommandExtractor {

    private final String SOURCE_CHARSET = "UTF-8";

    @Override
    public String[] extractCommands(final Reader reader) {
        String[] lines = super.extractCommands(reader);

        Charset charset = Charset.defaultCharset();
        if (!charset.equals(Charset.forName(SOURCE_CHARSET))) {
            for (int i = 0; i < lines.length; i++) {
                try {
                    lines[i] = new String(lines[i].getBytes(), SOURCE_CHARSET);
                } catch (UnsupportedEncodingException e) {
                    throw new IOError(e);
                }
            }
        }

        return lines;
    }
}

你可以改变的价值 SOURCE_CHARSET 使用导入文件使用的另一种编码。


2
2018-02-11 16:14