3

I want to generate DB structure from my Java classes

jpa.generate-ddl: true
jpa.ddl-auto: true

Also, I need to run SQL script before application will up because I have @PostConstruct methods where I use these data.

Can you show an example how to do it in Spring Boot?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Are you using hibernate? try using spring.jpa.hibernate.ddl-auto=option The list of option which is used in the spring boot are validate: validate the schema, makes no changes to the database. update: update the schema. create: creates the schema, destroying previous data. create-drop: drop the schema at the end of the session So for avoiding the data lose you use update. – Sadiq Ali May 31 '17 at 16:12

1 Answers1

0

A simple spring boot app with the required functionality can be found at. https://github.com/salilotr89/Spring-boot-postgres-dbinit

Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath).

In addition Spring Boot will load the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform, e.g. you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql etc.).

Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if the scripts cause exceptions the application will fail to start. The script locations can be changed by setting spring.datasource.schema and spring.datasource.data, and neither location will be processed if spring.datasource.initialize=false.

To disable the fail-fast you can set spring.datasource.continue-on-error=true. This can be useful once an application has matured and been deployed a few times, since the scripts can act as ‘poor man’s migrations’ — inserts that fail mean that the data is already there, so there would be no need to prevent the application from running, for instance.

If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

For Reference: Spring Boot - Loading Initial Data

Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22
  • I tried it. 1.It doesn't work for me 2. I should do it only in case if certain profile activated – gstackoverflow May 31 '17 at 16:05
  • Are you using maven? You can use flyway if you are, it will help your application scale in a good way. For details : https://egkatzioura.com/2015/05/14/database-migration-with-flyway/ – Sadiq Ali May 31 '17 at 16:08
  • I use gradle and flyway. – gstackoverflow May 31 '17 at 16:18
  • This tutorial has it all, let me know if you face an issue following it. https://www.mkyong.com/spring-boot/spring-boot-spring-data-jpa-oracle-example/ – Sadiq Ali May 31 '17 at 16:21
  • I created a simple spring boot application that does what you want, I have uploaded it on git and have updated the answer to reflect that. Hope it helps. – Sadiq Ali May 31 '17 at 16:56