Hibernate encodes wrong while persisting objects [UTF-8]
Asked Answered
S

3

13

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>
Saboteur answered 1/8, 2012 at 11:18 Comment(0)
R
31

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>
Rafaelle answered 2/8, 2012 at 7:16 Comment(3)
After trying all Hibernate and MySQL configuration, that filter approach worked. thanks!Lengel
<param-value>UTF8</param-value> must be <param-value>UTF-8</param-value> (dash is missed)Stronghold
this is not working for postgreSQL database. What steps should i take for postgreSQL database? See my question here #33604879Sulphathiazole
C
1

If you have added the

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

OR

alter database my_database default charset utf8 collate utf8_general_ci;

Note that i was using cyrillic strings.

Convoke answered 2/6, 2015 at 13:37 Comment(0)
C
0

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

Chronicles answered 1/8, 2012 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.