要求與條件
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 增加參數,沒用。
換個思路,找到一個MySQL55Dialect
application.properties 中增加
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
OK,解決