简体   繁体   English

使用Spring 4 + Hibernate 4插入DB非英语字符

[英]Insert into DB non-English characters with Spring 4 + Hibernate 4

I have Spring 4 + Hibernate 4 + MySQL web application. 我有Spring 4 + Hibernate 4 + MySQL Web应用程序。 I need initialize DB tables when app are starting. 应用启动时,我需要初始化数据库表。 For this goal I use import.sql file and set Hibernate hbm2ddl.auto to create . 为此,我使用import.sql文件并将Hibernate hbm2ddl.auto设置为create When Hibernate execute requests I have hieroglyphs in DB. 当Hibernate执行请求时,我在DB中有象形文字。 I try use 我尝试使用

<property name="url" value="jdbc:mysql://localhost:3306/foxrest_db?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8" />

for connection, but it has no effect. 用于连接,但没有效果。

I think Spring open files with standard system environment encoding (in my case it's Windows 8.1, encoding: win1251), that's why all Hibernate configurations has no effect. 我认为Spring使用标准系统环境编码(在我的情况下是Windows 8.1,编码:win1251)打开文件,这就是为什么所有Hibernate配置都无效的原因。

My question is: 我的问题是:

1.How can I fix this? 1.我该如何解决?

2.If I will use PostgreSQL in future what I will change in DB connection or another configs? 2.如果将来我要使用PostgreSQL,我将在数据库连接或其他配置中进行哪些更改?

Here is my sources: 这是我的资料来源:

spring.xml spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.foxresult" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"  value="jdbc:mysql://localhost:3306/foxrest_db" />
        <property name="username" value="lekarto" />
        <property name="password" value="1" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

hibernate.cfg.xml hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
        <property name="hibernate.cache.default_cache_concurrency_strategy">transactional</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="hibernate.connection.useUnicode">true</property>
        <property name="hibernate.connection.characterEncoding">UTF-8</property>
        <property name="hibernate.connection.charSet">UTF-8</property>
    </session-factory>
</hibernate-configuration>

import.sql import.sql

INSERT INTO `departments` (`name`) VALUES ('Developers'), ('QA'), ('Managers'), ('Support');
INSERT INTO `employees` (`first_name`, `last_name`, `salary`, `sex`, `department_id`) VALUES ('Sergey', 'Fedorov', '100', 1, '1'), ('Иван', 'Демидов', '120', 1, '1'), ('الحسيب', 'عبد', '140', 1, '2'), ('Angelina', 'Feofilaktova', '160', 0, '3'), ('湧', '阮', '180', 0, '3'), ('Test', 'Ivanovna', '180', 0, NULL);

I think the character encoding property for the JVM needs to be set. 我认为需要设置JVM的字符编码属性。

Have you tried starting the Java App with this: java -Dfile.encoding=UTF-8 您是否尝试以此启动Java App: java -Dfile.encoding=UTF-8

If you are using Tomcat, then you may have to set this System property in one of the Tomcat's (catalina) script files. 如果使用的是Tomcat,则可能必须在Tomcat的(catalina)脚本文件之一中设置此System属性。

Note: In past I have used only ISO-8559-1. 注意:过去我仅使用ISO-8559-1。 both the Java app, and the Database were set to ISO-8559-1 Java应用程序和数据库都设置为ISO-8559-1

UPDATE: 更新:

If maven surefire plugin is being used, then system property should be given according to this link: http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html 如果正在使用maven surefire插件,则应根据此链接提供系统属性: http : //maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

Note that the website mentions special case for JVM system properties: <argLine>-Djava.endorsed.dirs=...</argLine> 请注意,该网站提到了JVM系统属性的特殊情况: <argLine>-Djava.endorsed.dirs=...</argLine>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM