Java Hibernate:创建 Mysql InnoDB 表而不是 MyISAM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1459265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: Create Mysql InnoDB tables instead of MyISAM
提问by David Tinker
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".
如何让 Hibernate(使用 JPA)创建 MySQL InnoDB 表(而不是 MyISAM)?我找到了在使用 Hibernate 生成 SQL 文件来创建表时有效的解决方案,但没有任何“即时”有效的解决方案。
采纳答案by Pascal Thivent
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
回答by skaffman
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
回答by James Barwick
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. I guess I could have extended the class.
我最终创建了自己的类....将 org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter 的全部内容复制到我自己的类文件中,并修改了一个子程序的输出以将 MySQL 方言更改为 MySQL5InnoDBDialect。我想我可以延长课程。
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. (no component scanning, my classes are listed in META-INF/persistence.xml (the default location))
然后我的类用于“数据库”适配器 bean。(没有组件扫描,我的类在 META-INF/persistence.xml(默认位置)中列出)
回答by James Barwick
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...
所以,你不需要扩展或改变一个类......在我回答之前应该阅读原始 HibernateJpaVendorAdapter 的原始源代码。这让我进入了“databasePlatform”属性......
回答by Jules
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 5.2.8 开始,Mysql*InnoDBDialect
不推荐使用其他答案使用的类。新的解决方案是设置以下属性:
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/。
回答by Oliver Sahner
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
回答by OzzyTheGiant
If you are using Hibernate 5.2.8+, try using the MySQL55Dialect, which according to the link provided by Jules, sets innoDB by default as the storage engine.
如果您使用的是 Hibernate 5.2.8+,请尝试使用 MySQL55Dialect,根据 Jules 提供的链接,默认情况下将 innoDB 设置为存储引擎。
回答by Ajay Kumar
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.
传统上,MySQL 使用非事务性 MyISAM 存储引擎,这是所有早于 MySQL55Dialect 的方言的默认存储引擎。从 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. - not just 5.
上面的第 55 条通知。- 不只是 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.
希望能帮助到你。
回答by Yura Galavay
For newer versions, you can use
对于较新的版本,您可以使用
hibernate.dialect.storage_engine=innodb
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Other options for hibernate.dialectcan be MySQL55Dialector MySQL57Dialect
hibernate.dialect 的其他选项可以是MySQL55Dialect或MySQL57Dialect
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
回答by SalutonMondo
in case you choose application.yml
如果您选择 application.yml
spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect