Spring boot and JUnit with H2 JPA calls "pg_class" not found

I currently have a spring boot application that contains 2 configuration files: application.yaml and application-test.yaml . The test application profile is loaded correctly, and any settings in this file work properly.

However, I have a problem with one parameter, in particular spring.jpa.hibernate.ddl-auto = 'update'. When this parameter is defined in the application.yaml file , it crashes my JPA unit tests with the exception “PG_CLASS table not found”. I tried to override this parameter with different values ​​in the application-application configuration file to no avail.

My complete configuration files are as follows:

application.yaml

#Configure Postgres backend datasource
spring.datasource.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://ec2-54-75-233-146.eu-west-1.compute.amazonaws.com:5432/xxxx?user=xxxx&password=xxxxx&sslmode=require
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults: false
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto: update

test.yaml applications

spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_default: false
spring.jpa.database-platform: org.hibernate.dialect.H2

Uncommenting the "spring.jpa.hibernate.ddl-auto: update" parameter in my application.yaml file fixes the problem. But I would like it to be included. Any ideas?

+5
source share
2 answers

It seems that the above error is caused by the fact that Hibernate used the wrong dialect (PostgreSQL instead of H2).

Maybe try adding the following to application-test.yaml:

spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.H2Dialect
+2
source

These properties have helped me.

hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=create

spring.datasource.usernam: sa
spring.datasource.url: jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DB_CLOSE_ON_EXIT=FALSE;
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

spring.main.allow-bean-definition-overriding=true
0
source

Source: https://habr.com/ru/post/1656685/


All Articles