Hibernate在持久化对象时编码错误[UTF-8] [英] Hibernate encodes wrong while persisting objects [UTF-8]

查看:111
本文介绍了Hibernate在持久化对象时编码错误[UTF-8]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对UTF-8编码问题,同时坚持我的模型对象。在土耳其语中,ı是一封信。还有一些其他的土耳其字符包含在UTF-8编码中。虽然我坚持使用模型对象,但所有'ı'字符都以''的形式持久保存到数据库中。我在Ubuntu Linux 64位操作系统上使用 MySQL 5.5 。此外,我已经设置了休眠& c3p0连接编码属性也转换为UTF-8。当我调试时,来自客户端的数据是真实的。



这是我的配置,如果有人能帮助我,我会非常高兴。



预先致谢。




Spring& amp ; Hibernate Config

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean > 
< property name =dataSource>< ref local =dataSource/>< / property>
< property name =packagesToScanvalue =com.tk.dms.model/>
< property name =hibernateProperties>
<道具>
< 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属性 - >
< 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>
< /道具>
< / property>
< / bean>


解决方案

尝试在数据源中设置编码
$ b

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

你也确定表单的输入是正确编码的吗?你在你的弹簧应用程序中使用过滤器吗?



过滤器应放置在您的web.xml文件中:

 < filter> 
< filter-name> SetCharacterEncodingFilter< / filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter< / filter-class>
< init-param>
< / param-name>编码< / param-name>
< param-value> UTF8< /参数值>
< / init-param>
< init-param>
< param-name> forceEncoding< / param-name>
< param-value> true< /参数值>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> SetCharacterEncodingFilter< / filter-name>
< url-pattern> *< / url-pattern>
< / filter-mapping>


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.

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

Thanks in advance.


Spring & Hibernate Config

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

解决方案

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.

The filter should beplaced in your web.xml file:

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

这篇关于Hibernate在持久化对象时编码错误[UTF-8]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆