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

问题描述 投票:12回答:4

我正在使用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命令启动它,我的问题就消失了。

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

hibernate utf-8 character-encoding
4个回答
12
投票

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

因此,换句话说,您的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 doesn't 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>
  • 如果是gradle:要在gradle中添加此属性,请将systemProperty 'file.encoding', 'UTF-8'添加到任务配置块。 (Thanks @meztihn

3
投票

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


3
投票

从版本5.2.3开始,Hibernate中有一个新属性用于这样的情况。

<property name="hibernate.hbm2ddl.charset_name" value="UTF-8" />

2
投票

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

我们假设导入文件是用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的值。

© www.soinside.com 2019 - 2024. All rights reserved.