简体   繁体   English

Hibernate:创建 Mysql InnoDB 表而不是 MyISAM

[英]Hibernate: Create Mysql InnoDB tables instead of MyISAM

How can I get Hibernate (using JPA) to create MySQL InnoDB tables (instead of MyISAM)?如何让 Hibernate(使用 JPA)创建 MySQL InnoDB 表(而不是 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".我找到了在使用 Hibernate 生成 SQL 文件来创建表时有效的解决方案,但没有任何“即时”有效的解决方案。

Can't you specify the Hibernate dialect and use你不能指定 Hibernate 方言并使用

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

Edit编辑

From MySQL version > 5.1 this should be从 MySQL 版本 > 5.1 这应该是

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

to avoid running into this issue Using "TYPE = InnoDB" in MySQL throws exception避免遇到此问题在 MySQL 中使用“TYPE = InnoDB”会引发异常

Go to this link:转到此链接:

mysql-dialect-refactoring mysql-方言重构

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.传统上,MySQL 使用非事务性 MyISAM 存储引擎,这是所有早于 MySQL55Dialect 的方言的默认存储引擎。 From MySQL55Dialect onwards, the InnoDB storage engine is used by default.从 MySQL55Dialect 开始,默认使用 InnoDB 存储引擎。

Put the following in your application.properties (or in your config):将以下内容放入您的 application.properties(或您的配置):

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

Notice 55 in above.上面的第 55 条通知。 - not just 5. - 不只是 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.如果没有,Hibernate 将尝试自动检测数据库方言,并选择最安全的 MySQL 方言,即 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-boot 2.0.0M7 以下确实对我有用(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.从 Hibernate 5.2.8 开始,不推荐使用其他答案使用的Mysql*InnoDBDialect类。 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.有关更多详细信息,请参阅http://in.relation.to/2017/02/20/mysql-dialect-refactoring/

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 hibernate.dialect 的其他选项可以是MySQL55DialectMySQL57Dialect

Just in case of Spring Boot 2以防万一 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.我试图在 Spring 3.2 中使用 hibernate4 并将其包装在 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.我最终创建了自己的类....将 org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter 的全部内容复制到我自己的类文件中,并修改了一个子程序的输出以将 MySQL 方言更改为 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:在 Spring 上下文配置中,我添加了:

<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.然后我的类用于“数据库”适配器 bean。 (no component scanning, my classes are listed in META-INF/persistence.xml (the default location)) (没有组件扫描,我的类在 META-INF/persistence.xml(默认位置)中列出)

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.所以,你不需要扩展或改变一个类......在我回答之前应该阅读原始 HibernateJpaVendorAdapter 的原始源代码。 That clued me into the "databasePlatform" property...这让我进入了“databasePlatform”属性......

in case you choose application.yml如果您选择 application.yml

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

Here are the properties from my persistence.xml that fixed it.这是我的persistence.xml 中修复它的属性。 You can use those in Spring or directly in Hibernate, whatever your dev stack:您可以在 Spring 中或直接在 Hibernate 中使用它们,无论您的开发堆栈如何:

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        <property name="hibernate.dialect.storage_engine" value="innodb"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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