5

I'm using Spring Boot 2.1 with Java 11. I am using Maven to build my artifacts. When running locally, I like the Spring JPA directives that let me create the database automatically ...

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true

I also like the directives that let me auto-create files ...

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

However when I combine both in my src/main/resources/application.properties ...

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

it seems the "spring.jpa.properties.javax.persistence" take precedence and my schema changes are not auto-run against the database. Is there a way to configure things such that both happen -- the changes get recorded to a file and automatically run against my database?

satish
  • 703
  • 5
  • 23
  • 52

1 Answers1

1

Add the database.action with javax.persistence as follows which will update the database schema according to models as explained in Database Schema Creation

application.properties

spring.jpa.properties.javax.persistence.schema-generation.database.action=update

Also recommended to change (Deprecated) PostgreSQLDialect dialect with PostgreSQL82Dialect or according to the version you are using. Dailects

application.properties

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • I'm using Spring Boot and I'm reading that persistence.xml is ignored by Spring Boot -- https://medium.com/@nieldw/jpa-spring-boot-will-not-search-for-meta-inf-persistence-xml-f28f14444d6d . Is there a way to include that property as part of the application.properties file? – satish Jul 12 '20 at 18:11
  • Yes it is, I have added for someone who is using the xml configurations. As your project consists of application properties file you only have to include `spring.jpa.properties.javax.persistence.schema-generation.database.action=update` as described in the answer – Romil Patel Jul 12 '20 at 18:16