报错信息主要内容:
Cant DROP [xxx];check that column/key exists
报错信息查阅资料:
在查阅各类博客后,发现一篇比较关键的信息:【JPA在ddl-auto=update时,首次执行报错Cant DROP; check that column/key exists】
在其中说明了问题是因为一个配置项导致的,但是该文博主并没有给出解决方法的实际内容,于是回归源码查阅,根据类名及关键信息,查到里面有一个配置为:
package org.hibernate.tool.schema.internal;
public abstract class AbstractSchemaMigrator implements SchemaMigrator {
…………
private UniqueConstraintSchemaUpdateStrategy uniqueConstraintStrategy;
}
查阅该枚举后,发现:
于是锁定问题原因是因为默认配置是:先删除+后创建。
那么问题的解决就应该考虑使用第二个:RECREATE_QUIETLY,直接尝试创建,并忽略异常。
问题解决方法:
在application.yml中配置上述所说的配置,但是由于这个配置并没有提示,于是经过查询后,发现在:
package org.hibernate.cfg;
public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
……
String UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY = "hibernate.schema_update.unique_constraint_strategy";
……
}
于是在application.yml中配置:
spring:
## jpa配置
jpa:
hibernate:
ddl-auto: ${JPA_HIBERNATE_DDL_AUTO:update}
open-in-view: ${JPA_OPEN_IN_VIEW:true}
show-sql: ${JPA_SHOW_SQL:false}
properties:
hibernate:
schema_update:
## 该配置是本篇博文的重点,设置后,报错不再出现
unique_constraint_strategy: RECREATE_QUIETLY


![JPA:第一次自动建表时,报错Cant DROP [xxx];check that column/key exists JPA:第一次自动建表时,报错Cant DROP [xxx];check that column/key exists](http://www.mshxw.com/aiimages/31/995788.png)
