SpringBoot spring.jpa hibernate自动创建表引擎为innodb

目录

  • 要求与条件
    • 默认下
    • 改为Innodb

要求与条件

SpringBoot 2.0.6
hibernate-core 5.2.17.Final
Java JDK8

Eclipse Java EE IDE for Web Developers. Version: 2018-09 (4.9.0)

默认下

application.properties 中

spring.jpa.generate-ddl=auto

hibernate 会自动根据实体类创建表,但创建的是默认的存储引擎 MyISAM,不支持外键也不支持事务,不符合要求。

改为Innodb

从日志输出内容中看

[main] org.hibernate.dialect.Dialect  : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect

MySQLDialect类实现MySQL方言,打开

public MySQLDialect() {
	super();

	String storageEngine = Environment.getProperties().getProperty( Environment.STORAGE_ENGINE );
	if(storageEngine == null) {
		storageEngine = System.getProperty( Environment.STORAGE_ENGINE );
	}
	if(storageEngine == null) {
		this.storageEngine = getDefaultMySQLStorageEngine();
	}
	else if( "innodb".equals( storageEngine.toLowerCase() ) ) {
		this.storageEngine = InnoDBStorageEngine.INSTANCE;
	}
	else if( "myisam".equals( storageEngine.toLowerCase() ) ) {
		this.storageEngine = MyISAMStorageEngine.INSTANCE;
	}
	else {
		throw new UnsupportedOperationException( "The " + storageEngine + " storage engine is not supported!" );
	}

看构造函数看,Environment.STORAGE_ENGINE (hibernate.dialect.storage_engine)决定使用引擎,在Environment.getProperties() 和 System.getProperty 中。
尝试在Environment中和 Program arguments 增加参数,没用。

SpringBoot spring.jpa hibernate自动创建表引擎为innodb_第1张图片SpringBoot spring.jpa hibernate自动创建表引擎为innodb_第2张图片

换个思路,找到一个MySQL55Dialect
SpringBoot spring.jpa hibernate自动创建表引擎为innodb_第3张图片
application.properties 中增加

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

OK,解决

你可能感兴趣的:(spring,db,mysql,hibernate)