dimanche 31 mai 2020

H2 ApplicationContext Fails when creating multiple tables

Encountered the following problem during unit testing with Spring-boot/H2 database:

Once I define more than one table in my schema.sql file - getting an error:

java.lang.IllegalStateException: Failed to load ApplicationContext


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/hiddenpath/target/test-classes/schema.sql]: # DROP TABLE IF EXISTS User; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "[*]# DROP TABLE IF EXISTS USER"; SQL statement:
# DROP TABLE IF EXISTS User [42000-200]

Scemas I have in schema.sql file :

CREATE TABLE User
(
    id      int auto_increment primary key,
    name    varchar(20) not null,
    surname varchar(20) not null,
    role    varchar(20) not null,
    email   varchar(30) not null
);

create table Dashboard
(
    id              int auto_increment primary key,
    name            varchar(20) not null,
    creation_date   datetime    not null,
    due_date        datetime    null,
    completion_date datetime    null
);

create table dashboard_user
(
    dashboard_id int null,
    user_id      int null,
    constraint dashboard_user_dashboard_id_fk
        foreign key (dashboard_id) references dashboard (id),
    constraint dashboard_user_user_id_fk
        foreign key (user_id) references user (id)
);

What's strange is that no error happening when i have only User table in the schema.sql, but problem appears once add more tables.

My properties file looks as follows:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.h2.console.path=/h2
spring.datasource.schema=classpath*:schema.sql
spring.datasource.data=classpath*:data.sql
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false

Appreciate your help

Aucun commentaire:

Enregistrer un commentaire