Home > Back-end >  Springboot JPA/ORM is not working when data.sql will be used
Springboot JPA/ORM is not working when data.sql will be used

Time:01-10

previously i've used SpringBoot 2.4.5 and now i've updated it to 2.7.7 In the past, i had my entities with a H2 and my data.sql script. At first my H2-Database has been created according entities, so i could see in my log something like:

CREATE Table Users(..) 

And after this, my data.sql script has been executed and i saw a log-statment like:

INSERT INTO Users(....)

After my update on 2.7.7 i dont see any logs like "CREATE Table Users...." I just see "INSERT INTO Users"-Statments.

I have already tried out different constellations of the properties:

spring.datasource.url=jdbc:h2:mem:redditDB
spring.datasource.name=redditDB
spring.sql.init.mode=embedded
#spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=create
#spring.sql.init.mode=embedded
#spring.datasource.initialize=true
#spring.sql.init.platform=h2
spring.sql.init.continue-on-error=false
spring.sql.init.data-locations=classpath:sql/dml/*.sql

I can only create my H2-Db according my Entities, if i disable:

#spring.sql.init.data-locations=classpath:sql/dml/*.sql

But i need to populate data in my H2 DB

CodePudding user response:

Make sure you add the lines down below in your properties file.

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create

you probably won't need these lines, because it is already the same as the default ones.

spring.sql.init.mode=embedded
spring.sql.init.continue-on-error=false

and you should not disable the data-locations line, because the insert SQL wouldn't appear.

CodePudding user response:

I was able to fix it. I needed to set a property:

spring.jpa.defer-datasource-initialization=true
  • Related