MySQL Hibernate 在持久化对象时编码错误 [UTF-8]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11758094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:21:50  来源:igfitidea点击:

Hibernate encodes wrong while persisting objects [UTF-8]

mysqlspringhibernatespring-mvcc3p0

提问by talha06

I'm facing with UTF-8 encoding problem while persisting my model objects. In Turkish '?' is a letter. Also there're some other Turkish characters that is included in UTF-8 encoding. While I persist my model objects, all '?' characters are persisted as '?' to DB. I'm using MySQL 5.5 on Ubuntu Linux 64-bit OS. Also I've already set hibernate & c3p0 connection encoding property to UTF-8 too. When I debug, the data comes from client is true.

我在保留模型对象时面临 UTF-8 编码问题。在土耳其语' ? ' 是一个字母。还有一些其他土耳其语字符包含在 UTF-8 编码中。当我坚持我的模型对象时,所有 ' ? ' 字符被保留为 ' ? ' 到 DB。我在 Ubuntu Linux 64-bit OS 上使用MySQL 5.5。此外,我也已经将 hibernate & c3p0 连接编码属性设置为 UTF-8。当我调试时,来自客户端的数据是真实的。

Here's my config and I'll be so happy if someone can help me.

这是我的配置,如果有人可以帮助我,我会很高兴。

Thanks in advance.

提前致谢。



Spring & Hibernate Config

Spring & Hibernate 配置

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="packagesToScan" value="com.tk.dms.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <!-- c3p0 properties -->
                <prop key="hibernate.c3p0.min_size">2</prop>
                <prop key="hibernate.c3p0.max_size">50</prop>
                <prop key="hibernate.c3p0.maxPoolSize">50</prop>
                <prop key="hibernate.c3p0.minPoolSize">2</prop>
                <prop key="hibernate.c3p0.initialPoolSize">2</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>                
            </props>
        </property>
    </bean>

回答by pawelccb

Try setting encoding in datasource

尝试在数据源中设置编码

 <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://127.0.0.1:3306/databaseName?characterEncoding=UTF-8</value>
    </property>
    <property name="username">
        <value>?</value>
    </property>
</bean>

Also are you sure that the input from forms is properly encoded? Do you use filter in your spring application? Run application in debug mode and check fields of your model object before persisting.

此外,您确定表单的输入已正确编码吗?您是否在 Spring 应用程序中使用过滤器?在调试模式下运行应用程序并在持久化之前检查模型对象的字段。

The filter should beplaced in your web.xml file:

过滤器应该放在你的 web.xml 文件中:

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

回答by a.valchev

If you have added the

如果您添加了

?characterEncoding=UTF-8

?characterEncoding=UTF-8

parameter to the connection string consequently (after the db and tables have already been created), you need to recreate your database. Actually in my case i recreated my db with:

因此,连接字符串的参数(在已创建数据库和表之后),您需要重新创建数据库。实际上在我的情况下,我重新创建了我的数据库:

CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;

OR

或者

alter database my_database default charset utf8 collate utf8_general_ci;

更改数据库 my_database 默认字符集 utf8 整理 utf8_general_ci;

Note that i was using cyrillic strings.

请注意,我使用的是西里尔文字符串。

回答by Ankit

Check what is the data stored in your bean. How is your bean populated?, is it through user input?, if yes, then you will have to write encoding in that page as UTF-8 as well and change your request and response parameter to UTF-8

检查存储在您的 bean 中的数据是什么。您的 bean 是如何填充的?是通过用户输入吗?如果是,那么您还必须在该页面中将编码编写为 UTF-8,并将您的请求和响应参数更改为 UTF-8