Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate SQL schema from Spring Boot entities?

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):

  • create table creation SQL queries scripts
  • create Spring entites containing corresponding annotations
  • run application : the flyway script generates the tables

Writing scripts AND entites can be time consuming, and without added value. Is it possible to do it only once?

Thanks

like image 222
frinux Avatar asked Jan 26 '17 13:01

frinux


People also ask

Where do I put schema in spring boot SQL?

You can simply create a import. sql file in src/main/resources and Hibernate will execute it when the schema is created.

What is schema SQL in spring boot?

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}.

How do I create a schema in H2 database in spring boot?

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.


1 Answers

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.

like image 183
giomendo Avatar answered Sep 19 '22 15:09

giomendo