Spring Boot is a good framework to develop quickly applications. However, when creating an application binded to database, it seems some of the work must be done twice (I'm using Flyway):
Writing scripts AND entites can be time consuming, and without added value. Is it possible to do it only once?
Thanks
You can simply create a import. sql file in src/main/resources and Hibernate will execute it when the schema is created.
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data. sql , respectively. In addition, Spring Boot processes the schema-${platform}.
We can define schema by creating a SQL file in the resource folder (src/main/resource). We can populate data in the table by creating a SQL file in the resource folder (src/main/resource). Spring Boot automatically picks up the data. sql file and run it against the H2 database during the application startup.
Just set theese properties on your configuration file:
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
The schema file will be generated automatically in the project root. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With