简体   繁体   中英

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

With spring-boot 2.0.0M7 following did work for me (mysqld 5.7)

spring.jpa.hibernate.use-new-id-generator-mappings: true
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

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.

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

如果您使用的是 Hibernate 5.2.8+,请尝试使用 MySQL55Dialect,根据 Jules 提供的链接,默认情况下将 innoDB 设置为存储引擎。

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))

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

in case you choose application.yml

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM