Mysql 创建名为';实体管理工厂&x27;在类路径资源中定义。调用init方法失败;
我使用flyway迁移数据库,定义为spring.jpa.hibernate.ddl auto=validate,从一个空数据库开始。编译Maven项目时,出现以下错误: 创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejbaconfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:初始化方法调用失败 详细错误如下所示:Mysql 创建名为';实体管理工厂&x27;在类路径资源中定义。调用init方法失败;,mysql,hibernate,maven,spring-boot,flyway,Mysql,Hibernate,Maven,Spring Boot,Flyway,我使用flyway迁移数据库,定义为spring.jpa.hibernate.ddl auto=validate,从一个空数据库开始。编译Maven项目时,出现以下错误: 创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejbaconfiguration.class]中定义的名为“entityManagerFactory”的bean时出错:初始化方法调用失败 详细错误如下所示: org.springframework
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]
Im使用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文件中,程序运行正常:
表
hibernate\u序列(显然)缺失,这就是失败的原因
为什么会这样
我没有亲自使用Hibernate/MySQL,但我发现Hibernate使用MySQL方言中的“序列表”策略来处理自动递增的值
看看
所以Hibernate本身不会生成任何东西,因为您的配置包含一行spring.jpa.generate ddl=false
另一方面,您确实有自动递增的列,因此hibernate需要以某种方式映射它们,并使用hibernate\u序列
表进行映射。我想说的是,在一般情况下,自行创建数据库对象的工具在概念上不能很好地使用Flyway
在决议方面。
尝试使用参数运行它,这些参数可以让JPA/Hibernate生成所需的表和其他对象。然后检查数据库并创建DDL,以便使用SQL“手动”生成这些对象
将这些添加到flyway并将防止hibernate的DDL生成的标志设置回false
另一种方法是,如果您认为您的MySQL版本支持其他实现自动增量的方法,则尝试将MySQL方言配置为不使用表策略。(正如我所说,我从未专门使用过MySQL——我无法评论它是否支持此功能以及如何支持此功能)。在您的应用程序中。属性:
改变这个
spring.jpa.generate ddl=false
到
spring.jpa.generate ddl=true
这将自动创建一个hibernate_序列,您不需要自己创建它
create table hibernate_sequence (next_val bigint) engine=InnoDB;
insert into hibernate_sequence values ( 1 );
insert into hibernate_sequence values ( 1 );