Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL UTF8 with Hibernate 3 and Spring

All my tables in the schema are set to UTF-8 as the default charset, but I can't manage to get Hibernate insert correctly symbols like "é" or "ñ" (they are inserted as "é" or "ñ").

My configuration is the following:

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="${db.url}"></property>
    <property name="username" value="${db.user}"></property>
    <property name="password" value="${db.password}"></property>
    <property name="driverClassName" value="${db.driver}"></property>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

I've tried adding ?useUnicode=true&characterEncoding=UTF-8 to the connection URL, but with no results... Any idea?

like image 697
Joaquín L. Robles Avatar asked May 19 '11 15:05

Joaquín L. Robles


1 Answers

Solved, it wasn't an Hibernate problem, Tomcat was not configured to encode incoming requests as UTF-8.

like image 82
Joaquín L. Robles Avatar answered Oct 15 '22 04:10

Joaquín L. Robles