Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot and JUnit with H2 JPA causes 'pg_class' not found

I currently have a spring boot application which contains 2 configuration files: application.yaml and application-test.yaml. The application-test profile is loaded correctly and any settings in that file are working as expected.

However i am having an issue with one setting in particular which is spring.jpa.hibernate.ddl-auto = 'update'. When this setting is defined in the application.yaml file it causes my JPA unit tests to fail with the exception "Table "PG_CLASS" not found". I have tried overriding this setting with different values in the application-test configuration file to no avail.

My full 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

application-test.yaml

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" setting in my application.yaml file does fix the issue. But i would like to keep this enabled. Any ideas?

like image 302
Lewis GC M Avatar asked Oct 04 '16 10:10

Lewis GC M


1 Answers

The above errror seems to be caused by Hibernate using the wrong dialect (PostgreSQL instead of H2).

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

spring.datasource.driverClassName: org.h2.Driver
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.H2Dialect
like image 59
Vlad Dinulescu Avatar answered Oct 09 '22 15:10

Vlad Dinulescu