0

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

Community
  • 1
  • 1
Eric Huang
  • 1,114
  • 3
  • 22
  • 43

2 Answers2

3

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

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Thank you i did this... but its still not saving the correct characters in to the MySQL – Eric Huang Jun 01 '16 at 23:28
  • Please provide `SELECT col, HEX(col) FROM ...` for some 'incorrect' cell. This should help in further diagnosis. – Rick James Jun 01 '16 at 23:42
  • Hummmm i updated my question please take a look... my bean and dao is quite straight forward... Thanks again – Eric Huang Jun 03 '16 at 19:20
  • What does the 'bad' output look like? Question marks? Truncated text? Mojibake? Other? – Rick James Jun 04 '16 at 06:01
  • Strange string; it does not match any pattern I know of. What was the string supposed to be? – Rick James Jun 06 '16 at 16:24
  • Chinese or Japanese characters. Thank you very much for helping and looking in to this really appreciated. – Eric Huang Jun 06 '16 at 22:13
  • Please show us some specific characters and what they turn into. – Rick James Jun 07 '16 at 04:05
  • "Double encoding" turns that into `試試`. Another path gets to `試試`. I don't see any way to get to `ææ¯.....`. Can you provide the hex from the program for `試試`. Utf8 would be `E8A9A6 E8A9A6`. I also chased big5, gbk, gbk2312, without success. – Rick James Jun 16 '16 at 20:45
  • Do you have another example? Maybe I could get lucky with it. – Rick James Jun 16 '16 at 20:46
1

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>
Eric Huang
  • 1,114
  • 3
  • 22
  • 43