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.