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:


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

16 thoughts on “UTF-8 with Hibernate 3.0 and MySQL”

  1. 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″/>

    1. you just need to encode the ampersand with &amp; on the persistence xml.


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

  3. 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’

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


  5. 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:


    In JSPs :

    in HTML :

    Even I am using :
    in Filter.

    I will highly appreciate…

  6. 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:


    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.

  7. Also, make sure the MySQL database is UTF-8 aware. For instance, you can create a UTF-8-aware MySQL database like this:

    create database hibernatetest
    DEFAULT COLLATE utf8_general_ci;


Leave a Reply

Your email address will not be published. Required fields are marked *