11

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

frinux
  • 2,052
  • 6
  • 26
  • 47
  • You can generate the database by using the appropriate Hibernate properties. However, this is only suitable for a development environment, not for actual production use (there is a possibility to update a DB with Hibernate ddl generation, but i wouldn't trust it in a real production environment). Also, by writing migration scripts, you can much better influence the migrations itself, even with value conversions etc.) – dunni Jan 26 '17 at 14:14
  • spring.jpa.hibernate.ddl-auto=update – dos4dev Oct 28 '20 at 15:07

2 Answers2

15

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.

fracz
  • 20,536
  • 18
  • 103
  • 149
giomendo
  • 159
  • 1
  • 5
1

You can also use JPA Buddy plugin. It has a "Show DDL" menu where you can visualize the sql script for a selected entity. Really useful when you want to avoid creating everything manually.

akuma8
  • 4,160
  • 5
  • 46
  • 82