Gok*_*enG 12 hibernate utf-8 character-encoding
我正在使用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>
Run Code Online (Sandbox Code Playgroud)
我的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;
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我使用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" />
Run Code Online (Sandbox Code Playgroud)
到persistence.xml.但它没有帮助.
修复:我最终解决了它.我正在使用Tomcat,这是腐败的重点,而不是hibernate或mysql.我用set JAVA_OPTS = -Dfile.encoding = UTF-8命令启动它,我的问题就消失了.
问题的标题现在变得误导了.对不起.
acd*_*ior 12
在为该文件创建阅读器时,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 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
Run Code Online (Sandbox Code Playgroud)在你的Maven插件中设置一个属性(可能是surefire,failsafe或者其他,取决于你如何运行导入hibernate文件的代码).示例surefire:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=UTF8</argLine>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
7608 次 |
| 最近记录: |