我正在使用flyway迁移定义为spring.jpa.hibernate.ddl-auto = validate的数据库,从一个空的数据库开始。编译Maven项目时,出现以下错误:
在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]中创建名称为'entityManagerFactory'的bean时出错:初始化方法的调用失败;
详细错误如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [hibernate_sequence]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at com.exam.twister.Application.main(Application.java:11) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE]
我正在使用Intellij IDEA和MySQL数据库
Application.properties是
spring.jpa.hibernate.ddl-auto=validate
spring.datasource.url=jdbc:mysql://localhost:3306/twister?allowPublicKeyRetrieval=true&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.schemas=twister
spring.datasource.username=*
spring.datasource.password=*
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.flyway.baseline-on-migrate=true
spring.freemarker.expose-request-attributes=true
V1__Init_DB.sql是
CREATE TABLE IF NOT EXISTS message(
id bigint not null auto_increment primary key,
filename varchar(255),
tag varchar(255),
text varchar(2048) not null,
user_id bigint
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS user_role(
user_id bigint not null,
roles varchar(255)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS usr(
id bigint not null auto_increment primary key,
activation_code varchar(255),
active tinyint(1) not null,
email varchar(255),
password varchar(255) not null,
username varchar(255) not null
) ENGINE=InnoDB;
alter table message
add constraint message_user_fk
foreign key(user_id) references usr (id);
alter table user_role
add constraint user_role_user_fk
foreign key(user_id) references usr (id);
更新
我将此字符串添加到了我的sql文件中,并且程序运行正常:
create table hibernate_sequence (next_val bigint) engine=InnoDB;
insert into hibernate_sequence values ( 1 );
insert into hibernate_sequence values ( 1 );
答案 0 :(得分:1)
表hibernate_sequence
(显然)似乎丢失了,这就是失败的原因。
现在为什么会发生?
我自己还没有使用过Hibernate / MySQL,但是我发现hibernate在mysql方言中使用“序列表”策略来处理自动递增值。
看看this question and answer in SO
由于您的配置包含一行spring.jpa.generate-ddl=false
另一方面,您确实有自动递增的列,因此休眠需要以某种方式映射它们,并且它使用hibernate_sequence
表。我要说的是,一般而言,自行创建数据库对象的工具在概念上不能与Flyway很好地配合。
在分辨率方面。 尝试使用让JPA / Hibernate生成所需表以及其他对象的参数运行它。然后检查数据库并创建DDL,以使用SQL“手动”生成这些对象。
将这些添加到飞行通道,并将阻止DDL生成休眠状态的标志设置为false。
另一种方法是,如果您认为您的MySQL版本支持实现自动增量的其他方法,则尝试将MySQL方言配置为不使用表策略。 (自从我说过以来,我从没有专门与MySQL合作-我无法评论它是否支持此功能以及如何支持。)
答案 1 :(得分:0)
在您的Application.properties中:
更改此
spring.jpa.generate-ddl = false
到
spring.jpa.generate-ddl = true
这将自动创建一个hibernate_sequence,而您不需要自己创建它。