MySQL UTF8 with Hibernate 3 and Spring

12,260

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

Share:
12,260
Joaquín L. Robles
Author by

Joaquín L. Robles

Software Engineering Artisan and Founder at rudol.ai. #SOreadytohelp

Updated on July 20, 2022

Comments

  • Joaquín L. Robles
    Joaquín L. Robles almost 2 years

    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?

  • Ricardo
    Ricardo over 12 years
    Concerning the Tomcat configuration you mention, what did you really do? URIEncoding="UTF-8" at server.xml?
  • Joaquín L. Robles
    Joaquín L. Robles over 12 years
    I did that, also I added a org.springframework.web.filter.CharacterEncodingFilter to web.xml..
  • sinoohe
    sinoohe about 11 years
    The answer 4 in this question explains how to do that: stackoverflow.com/questions/5928046/spring-mvc-utf-8-encodin‌​g