Eric Huang Eric Huang - 1 year ago 73
SQL Question

Hibernate Spring encode charater set wrong [UTF-8]

Development environment:

  • Spring 4.2x

  • Hibernate 4.3.11

  • Tomcat 8 server

  • MySQL 5.4

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
) VALUES ('ölm', 'lalalal');" the output on the index.jsp is correct.

I modified my hibernate config

<bean id="sessionFactory"
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<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 -->


<property name="packagesToScan">

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"

  4. I have also added filter as this post suggested... Spring MVC UTF-8 Encoding

* Update *
this is my bean and DAO


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



public class Offer{
private int id;
private String text;



public class OfferDao{
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


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 please leave a comment below

IMPORTANT!!! First filter on web.xml