Eric Huang
Eric Huang

Reputation: 1264

Hibernate Spring encode charater set wrong [UTF-8]

Development environment:

Before make the change to Hibernate there was no problem inserting, retrieving and displaying the characters. However after making the change in my DAO to use Hibernate oddly i can't seem to insert the correct character in to the MySQL DB.

I have made sure that MySQL Schema can indeed save UTF-8 Character set by using query "INSERT INTO spring_normalize.offers (text, users_username) VALUES ('ölm', 'lalalal');" the output on the index.jsp is correct.

I modified my hibernate config

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.connection.useUnicode">true</prop><!-- added -->
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop><!-- added -->
                <prop key="hibernate.connection.charSet">UTF-8</prop><!-- added -->

            </props>
        </property>

        <property name="packagesToScan">
            <list>
                <value>com.caveofprogramming.pring.web.dao</value>
            </list>
        </property>
    </bean>

This doesn't seem to work

Check list:

  1. DB schema is set to utf8 - utf8_unicode_ci.
  2. Hibernat config add charSet to UTF-8.
  3. jsp page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8
  4. I have also added filter as this post suggested... Spring MVC UTF-8 Encoding

* Update * this is my bean and DAO

Form

<sf:form method="POST" action="${pageContext.request.contextPath}/docreate" commandName="offer">
    <sf:input type="text" path="id" name="id" readonly="true" />
    <label for="text">Text</label>
    <sf:textarea id="text" name="text" row="3" path="text"></sf:textarea>
    <sf:errors path="text" cssClass="error"></sf:errors>
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
    <input type="submit" value="Submit">

</sf:form>

Offer

@Entity
@Table(name="offers")
 public class Offer{
  @Id
  private int id;
  private String text;

  getIn(){}
  .....
}

OfferDao

public class OfferDao{
   @Autowired
   private SessionFactory sessionFactory;

   public Session currentSession(){
    return sessionFactory.getCurrentSession();
   }

   public boolean create(Offer offer){
     int hiberReturn =(int) currentSession().save(offer);

     return hiberReturn >= 0;
   }
}  

Anyone who can help is much much appreciated... really.. many many thanks

Upvotes: 0

Views: 7654

Answers (2)

Eric Huang
Eric Huang

Reputation: 1264

I actually accidentally fixed this when I created another smaller web app with just a simple form input. For those who have the same problem go through the check list above and make sure you have everything.

The most important is to make sure the Character Encoding Filter has to be the first filter on web.xml reason..... I don't know the reason, so if you do... please leave a comment below

IMPORTANT!!! First filter on 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>UTF-8</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>

Upvotes: 1

Rick James
Rick James

Reputation: 142208

For Java (JDBC):

?useUnicode=yes&characterEncoding=UTF-8 in the getConnection() call.

For Hikari (perhaps):

spring.jpa.properties.hibernate.connection.characterEncoding=utf-8  
spring.jpa.properties.hibernate.connection.CharSet=utf-8  
spring.jpa.properties.hibernate.connection.useUnicode=true

Spring/Hibernate filter:

<form accept-charset="UTF-8">

Spring/Hibernate: <property name="url"
    value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true
           &connectionCollation=utf8_general_ci
           &characterSetResults=utf8&characterEncoding=utf-8"/>

"Spring": @RequestMapping(value = "/getRegion2",
    produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)

See also: https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html

Upvotes: 3

Related Questions