Reputation: 31
I'm using flyway to migrate the db, defined spring.jpa.hibernate.ddl-auto=validate ,start with an empty db. When I compile my Maven project, I got the following error:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed;
The detailed error is given below:
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 using Intellij IDEA and MySQL database
Application.properties is
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 is
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 );
Upvotes: 3
Views: 1057
Reputation: 66
In your Application.properties :
Change this
spring.jpa.generate-ddl=false
to
spring.jpa.generate-ddl=true
This will automatically creates a hibernate_sequence and you don't need to create it by yourself.
Upvotes: 0
Reputation: 42461
It looks like the table hibernate_sequence
(obviously) is missing and that's the reason of failure.
Now why does it happen?
I haven't worked with Hibernate/MySQL by myself but I've found that hibernate uses "sequence table" strategy in the mysql dialect for dealing with Auto incremented values.
Take a look at this question and answer in SO
So Hibernate doesn't generate anything by itself because your configuration contains a line spring.jpa.generate-ddl=false
On the other hand you do have auto-incremented columns so hibernate needs to map them somehow and it uses the hibernate_sequence
table for it. I would say that in general tools that create database objects by themselves do not play well with Flyway conceptually.
In terms of resolution. Try to run it with parameters that let JPA / Hibernate generate the required tables and maybe other objects. then inspect the database and create DDLs for generating these objects "manually" with SQL.
Add those to flyway and set the flag that prevents the DDL generation of hibernate back to false.
Another approach is trying to configure MySQL dialect to not use the table strategy if you think your version of MySQL supports other ways to implement auto-increment. (Since as I've said, I've never worked with MySQL specifically - I can't comment on whether it supports this feature feature or not and how).
Upvotes: 1