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".
-
Do you mean using the hbm2ddl config settings?skaffman– skaffman2009-09-22 10:29:05 +00:00Commented Sep 22, 2009 at 10:29
-
Yes. Apparently setting 'delimiter=type=InnoDB' works for the script output only. I tried it with 'hibernate.hbm2ddl.auto=create' and got MyISAM tables.David Tinker– David Tinker2009-09-22 10:31:59 +00:00Commented Sep 22, 2009 at 10:31
-
Created a jira for this: HHH-8050Ondra Žižka– Ondra Žižka2013-03-05 09:19:49 +00:00Commented Mar 5, 2013 at 9:19
11 Answers
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
4 Comments
hibernate.dialect.storage_engine=innodb. See in.relation.to/2017/02/20/mysql-dialect-refactoringspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect worked for me.spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect The above property worked for me adding into application.properties.Go to this link:
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.
1 Comment
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
Comments
As of Hibernate 5.2.8, the Mysql*InnoDBDialect classes used by the other answers are deprecated. The new solution is to set the following property:
hibernate.dialect.storage_engine = innodb
See http://in.relation.to/2017/02/20/mysql-dialect-refactoring/ for more details.
Comments
For newer versions, you can use
hibernate.dialect.storage_engine=innodb
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Other options for hibernate.dialect can be MySQL55Dialect or MySQL57Dialect
Just in case of Spring Boot 2
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
1 Comment
I was trying to use hibernate4 with Spring 3.2 and wrap it in JPA.
I ended up creating my own class.... copied the entire contents of the org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter into my own class file and modifying the output of one subroutine to change the MySQL Dialect to MySQL5InnoDBDialect. I guess I could have extended the class.
Anyway...
Modified as:
package com.imk.dao.hibernate;
public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
[ snip snip snip --- use the original code ]
protected Class determineDatabaseDialectClass(Database database) {
switch (database) {
case DB2:
return DB2Dialect.class;
case DERBY:
return DerbyDialect.class;
case H2:
return H2Dialect.class;
case HSQL:
return HSQLDialect.class;
case INFORMIX:
return InformixDialect.class;
case MYSQL:
return MySQL5InnoDBDialect.class;
case ORACLE:
return Oracle9iDialect.class;
case POSTGRESQL:
return PostgreSQLDialect.class;
case SQL_SERVER:
return SQLServerDialect.class;
case SYBASE:
return SybaseDialect.class;
default:
return null;
}
}
}
You might think this is a 'hack', but, I suppose it will work. In the Spring context config, I added:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="MosJPA" />
<property name="jpaVendorAdapter">
<bean class="com.imk.dao.hibernate.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
</bean>
</property>
</bean>
Then my class is used for the "database" adapter bean. (no component scanning, my classes are listed in META-INF/persistence.xml (the default location))
Comments
Here are the properties from my persistence.xml that fixed it. You can use those in Spring or directly in Hibernate, whatever your dev stack:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.dialect.storage_engine" value="innodb"/>
Comments
Oh, boy....sorry guys... more Googling gives another search result:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="MosJPA" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
</property>
</bean>
So, you don't need to extend or change a class...should have read the original source code of the original HibernateJpaVendorAdapter a bit further before I answered. That clued me into the "databasePlatform" property...