This blog is mainly about Java...

Friday, July 4, 2008

Generating InnoDB engine and UTF-8 for MySQL using hibernate

I found these blogs:

http://blog.tremend.ro/2007/08/14/how-to-set-the-default-charset-to-utf-8-for-create-table-when-using-hibernate-with-java-persistence-annotations


and

http://neoexpedition.wordpress.com/2007/11/01/persisting-japanese-characters-in-db-using-hibernate

They had the same problem as me, on how to generate InnoDB engine and UTF-8 for MySQL through hibernate.

This is the solution for doing it:

In persistence.xml or hibernate.cfg.xml (depending on what you are using), you add this line to get InnoDB as engine.

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>


or

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect">


if you are using MySQL 4.x.

To get utf8 as default charset, you have to create the database as default charset utf8 and hibernate will do that automatically for you.

create database foo default charset utf8;

No comments:

Labels