I'm migrating my existing Spring Boot 2.1.x
application to 2.6.x
. I want the following behavior (for integration tests which uses embedded H2 database), which is working fine in 2.1.x
(obviously) but I'm struggling to achieve the same behavior in 2.6.x
.
- Create DDL automatically (without any custom SQL script)
- Execute custom DML (data.sql)
- Initialize beans which implements
InitializingBean
and use data populated in step #2. Eg. Cache database table records for application to use later
Problem #1 (Solved)
2.6.x
was no longer created DDL automatically by default. So adding below properties explicit seemed to be the solution.
spring.jpa.generate-ddl = true
spring.jpa.hibernate.ddl-auto = create-drop
This solves Step #1 with 2.6.x
.
Problem #2 (Solved)
Starting from 2.5
, Boot was running scripts in data.sql
before Hibernate is initialized. Hence, data.sql
was failing. To make Boot wait for DDL creation to complete, adding below property seemed to be the solution.
spring.jpa.defer-datasource-initialization = true
This solves *Step #2 with 2.6.x
.
Problem #3 (Need solution)
Seemingly because of setting spring.jpa.defer-datasource-initialization
to true
, Step #3 is happening before Step #2 i.e. in 2.6.x
. This cause beans to initialize with empty database tables e.g. Cache is populated with no entries. These are causing my tests to fail.
I played with properties like spring.jpa.defer-datasource-initialization
, spring.data.jpa.repositories.bootstrap-mode
and spring.sql.init.mode
to workaround this without any luck.