Hibernate: Create Mysql InnoDB tables instead of MyISAM

How can I get Hibernate (using JPA) to create MySQL InnoDB tables (instead of MyISAM)? I have found solutions that will work when using Hibernate to generate an SQL file to create the tables, but nothing that works "on the fly".


Can't you specify the Hibernate dialect and use

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

Edit

From MySQL version > 5.1 this should be

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

to avoid running into this issue Using "TYPE = InnoDB" in MySQL throws exception


Go to this link:

mysql-dialect-refactoring

It clearly says :

Traditionally, MySQL used the non-transactional MyISAM storage engine, and this is the default storage engine for all Dialects that are older than MySQL55Dialect. From MySQL55Dialect onwards, the InnoDB storage engine is used by default.

Put the following in your application.properties (or in your config):

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

Notice 55 in above. - not just 5.

And you can see it in the console too:

Hibernate: create table users_events (user_id bigint not null, event_id bigint not null) engine=InnoDB
Hibernate: create table users_roles (user_id bigint not null, role_id bigint not null) engine=InnoDB

Hope it helps.


Are you specifying the dialect setting in your hibernate configuration? If not, then Hibernate will attempt to auto-detect the database dialect, and will choose the safest MySQL dialec, which is MySQL 4 MyISAM.

You can give it a specific dialect, by adding this to your hibernate properties:

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect