What are fixtures? Fixed data that is always present in the database. You can think of it as the initial dataset.
application.properties
1 2 3 4 5 6 |
spring.jpa.hibernate.ddl-auto=create spring.jpa.generate-ddl=true spring.jpa.open-in-view=false spring.sql.init.mode=always spring.sql.init.data-locations=classpath:db/data.sql spring.jpa.defer-datasource-initialization=true |
The documentation speaks of “the root classpath”, not knowing Java well I had no idea what that means and an answer on Stackoverflow was of course wrong.
It’s not the “src” dir, but the “resources” dir.
So with the above configuration we set spring.sql.init.data-locations=classpath:db/data.sql
which means we create a directory named “db” under src/main/resources
.
Also we create a file named “data.sql”.
If you have spring.jpa.hibernate.ddl-auto=create
but not spring.jpa.defer-datasource-initialization=true
, data.sql will be executed before the schema is created by JPA and then the tables will be destroyed and created.
data.sql obviously contains sql statements like INSERT INTO profiles (name) VALUES('Darko')
.
data.sql is only for data, don’t use it for schema.
The location setting for schema is spring.sql.init.schema-locations=
e.g. spring.sql.init.schema-locations=classpath:db/schema.sql
.
Since you’re using schema.sql you don’t need to defer data initialization.
Details at the Spring Boot JPA reference documentation