mysql - 创建类路径资源中定义的名称为 'entityManagerFactory' 的 bean 时出错。init 方法调用失败;

标签 mysql hibernate maven spring-boot flyway

我正在使用 flyway 迁移数据库,定义为 spring.jpa.hibernate.ddl-auto=validate ,从一个空数据库开始。当我编译我的 Maven 项目时,出现以下错误:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed;

详细错误如下:

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);

UPDATE

I added this strings to my sql file and programm worked:

create table hibernate_sequence (next_val bigint) engine=InnoDB;
insert into hibernate_sequence values ( 1 );
insert into hibernate_sequence values ( 1 );

最佳答案

看起来 hibernate_sequence 表(显然)丢失了,这就是失败的原因。

为什么会这样呢?

我自己没有使用过 Hibernate/MySQL,但我发现 Hibernate 在 mysql 方言中使用“序列表”策略来处理自动递增的值。

看看this question and answer in SO

所以 Hibernate 不会自己生成任何东西,因为你的配置包含一行 spring.jpa.generate-ddl=false

另一方面,您确实有自动递增的列,因此 hibernate 需要以某种方式映射它们,并且它使用 hibernate_sequence 表。我想说的是,一般来说,自行创建数据库对象的工具在概念上不能很好地与 Flyway 配合使用。

在分辨率方面。 尝试使用让 JPA/Hibernate 生成所需表和其他对象的参数来运行它。然后检查数据库并创建 DDL,以便使用 SQL“手动”生成这些对象。

将它们添加到 flyway 并将阻止 hibernate 的 DDL 生成的标志设置回 false。

另一种方法是如果您认为您的 MySQL 版本支持其他实现自动增量的方法,则尝试将 MySQL 方言配置为不使用表策略。 (因为正如我所说,我从未专门使用过 MySQL - 我无法评论它是否支持此功能特性以及如何支持)。

关于mysql - 创建类路径资源中定义的名称为 'entityManagerFactory' 的 bean 时出错。init 方法调用失败;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59081550/

相关文章:

sql - 在 MySQL 中加入两个相似的表

mysql:找到最高平均分

java - 在 JSP 中编写 Hibernate 查询会给出 java.lang.NullPointerException

java - maven:依赖编译错误

java - RoboVM 错误 - dyld : Library not loaded

php - 使用 end($array) 检索数组中的最后一行

php - 如何在 codeigniter 的模型中获取购物车的结果?

spring - 在dbcp + spring + hibernate + jdbc中禁用准备好的语句?

Hibernate Validator - 在实体验证失败后设置默认值

plugins - 仅在maven-war-plugin中复制修改的文件