0

I am facing a bit of trouble to get utf8 data out of hibenate. I am using RDS database. I have created a table with following syntax:

CREATE TABLE `Test' (
`id` char(36) NOT NULL
)DEFAULT CHARSET=utf8;

And this is my following configuration of hibernate:

<prop key="hibernate.connection.url">${JDBC.databaseURL}</prop>
<prop key="hibernate.connection.username">${Username} </prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>

I have inserted a row in db table manually in Japanese , and I could see that data is present in Japanese when I do select * from Test;

But when I query from hibernate, it is coming as garbage. I do not get back correctly. Can you please help what I am doing wrong here?

  • provide encoding with DB connection URL, jdbc:mysql://localhost/mydb?characterEncoding=UTF-8 – KhAn SaAb Jul 25 '17 at 14:11
  • What kind of "garbage"? There are about 5 cases listed in http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Jul 26 '17 at 02:32
  • Hi Rick, I am getting following data "ç§\u0081ã\u0081®å\u0090\u008då‰\u008dã\u0081¯ soumya". I saved "私の名前は soumya" i.e. (My name is for soumya.) I am using a simple java application. After console printing the follwing character, I was examining hibernate variables' values in debug mode. Still I was getting those values only. – Soumya Prasad Ukil Jul 26 '17 at 10:08
  • Something -- not MySQL -- did the equivalent of "utf8_encode". – Rick James Jun 19 '22 at 17:34

1 Answers1

0

Apart from hibernate configuration you need to set with filters.

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</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>

hibernate configuration.

<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
  • Hi khAn, are you thinking I am using any webapps? I believe the above configuration to be added in web.xml file. But I am using any web service at the moment. I created a small java application,where I am trying to connect to RDS using hibernate. That's where I am facing the problem. – Soumya Prasad Ukil Jul 26 '17 at 10:11
  • @SoumyaPrasadUkil, try by configuring UTF along with your jdbc URL in properties file. For eg: – KhAn SaAb Jul 26 '17 at 11:04