UTF-8 with Hibernate 3.0 and MySQL

Hi again, I’m now tackling something I’ve been meaning to do for ages (and probably should have done before I started the project) and that is to use Hibernate rather than rolling my own SQL.

I’m reading the book “Hibernate in Action” by Christian Bauer and Gavin King and have been trying it out. The book relates to Hibernate 2.0 and 2.1 so doesn’t include annotations which is a bit of a shame, but it’s still a good start.

Unfortunately as readers of this blog will know, I’m trying to put UTF-8 strings into the database so you can imagine my disappointment when I saw the familiar question marks appearing where there should be interesting new characters.

The fix turned out to be not so difficult to find however, thanks to Philip Whirlycott’s blog posting More UTF-8 head-thumping with Hibernate 3 (obviously I’m not the only one struggling with these issues).

With Hibernate 3 (maybe not with 2.1, I’m not sure), you need to add some extra connection parameters:

jdbc:mysql://localhost/mydb?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

If you put this in your hibernate.cfg.xml file for the JDBC URL it works and you can save the UTF-8 correctly.

Tags: , , ,

13 Responses to “UTF-8 with Hibernate 3.0 and MySQL”

  1. Michael Bar-Sinai says:

    Thanks for this useful post – I had the same problem and didn’t know what to do.

    One update for using JPA: not all the parameters are needed and they cause an exception while hibernate tries to parse the persistence.xml file. For me, the following work fine (for hebrew):

    <property name=”hibernate.connection.url” value=”jdbc:mysql://localhost/second_homes?characterEncoding=UTF-8″/>

  2. Michael Bar-Sinai says:

    jdbc:mysql://localhost/mydb?characterEncoding=UTF-8

  3. BISOِ says:

    Thanks a lot, you post where very helpfull

    BISO

  4. Thanks for your suggestion, I’ll use that with Grails.

  5. shawary says:

    thanks a lot for your post …. i steel has this problem . i am using hibernate3 with spring and jdbc:mysql://localhost/mydb?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 dose not work 4 me please advice.

  6. moien says:

    thanx for thiss usefull note.
    but i still have a problem inserting persian characters

    Hibernate: insert into Stocks (symbol, corpName, maxPrice, minPrice, lastPrice, finishedPrice, yesterdayPrice, changed, percent, bestRequest, bestOffer) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    765 [main] WARN org.hibernate.util.JDBCExceptionReporter – SQL Error: 1366, SQLState: HY000
    765 [main] ERROR org.hibernate.util.JDBCExceptionReporter – Incorrect string value: ‘\xD8\xB4\xD8\xA7\xD8\xB1…’ for column ’symbol’

  7. moien says:

    SOLVED!
    i just had to drop the db and create it again with new unicode collation

  8. Kris says:

    Took me a little while to work out that you need to escape the ‘&’ with &amp; in the hibernate.cfg.xml

    useUnicode=true&amp;characterEncoding=UTF-8

  9. In Hibernate 3.0 with MySQL 4 and mysql-connector 3.0.8, UTF-8 is not working…
    core mysql APIs are fetching and inserting utf8 characters correctly, but Hibernate session is not selecting or inserting special characters. I have tried it with Hibernate 3.0 + Mysql 5 + mysql-connector 5.0.8, working fine… but not with above config

    Please help me, how can I insert special characters (UTF-8) using this config : Hibernate 3.0 + Mysql 4.0.1 + mysql-connector 3.0.8

    my hibernat-cfg.xml file contain:

    true
    UTF-8

    In JSPs :

    in HTML :

    Even I am using :
    request.setCharacterEncoding(“UTF-8″);
    in Filter.

    I will highly appreciate…

  10. Andrew Yinger says:

    after much struggling this is what works for me:

    1. yes, you can modify the JDBC URL, but it’s a little clunky and requires just the right string, e.g.:
    jdbc:mysql://localhost/db?useUnicode=true&characterEncoding=utf8 (assuming this is embedded in XML)

    2. a litter clearer, but not well documented at all, are these additional connection settings in the hibernate cfg XML:

    utf8
    true

    NOTE: both charSet and useUnicode are required for latest version of hibernate and mysql. it took me quite a while to figure this out.

    again, either 1. or 2. will work, but my preference is for #2.

    i wonder when the world will finally understand and make encoding easier…..

    frustrated, but struggled and *finally* got it right.

Leave a Reply