Jan 22 2008

Persisting Japanese Characters in DB using Hibernate

Posted in Hibernate, MySQL on Tuesday, January 22, 2008 at 5:54

I was not able to store user input [with Japanese characters] in database using Hibernate properly. All other data except the field that store data in Japanese format was getting populated fine.

As per the first doubt - did I declare that column to accept data in UTF-8 format? Yes, I have that column mapped for UTF-8 input. In fact, when I entered data using MySQL Query Browser, it is getting stored right.

Second thought was - am I getting uncorrupted data across the layers i.e. from presentation layer -> business layer -> data access layer. That too was correct.

That left me with only one possibility [may not be, but I was under such impression] - when hibernate is trying to persist data into database, it is corrupting it and I am getting garbled values. I’ve got some hint from hibernate tutorial. According that I can specify hibernate.connection.charSet as property in hibernate configuration file. I tried that but no help.

My further search led me to a blog where a solution was suggested by extending MySQLDialect. But if I understood correctly, his/her problem was bit different that what I was facing. I got another hint from the comments on the same blog [though the person who left the comment said it didn’t work for him :-(]. It was a suggestion to supply character encoding to connection URL i.e. jdbc:mysql://localhost/pas?characterEncoding=UTF-8. Lucky enough, it works for me!

After appending character encoding to my connection URL, now I am able to persist data properly!

No responses yet