How to generate a ddl creation script with a modern Spring Boot + Data JPA and Hibernate setup?

HibernateSpring BootSpring DataSpring Data-Jpa

Hibernate Problem Overview


Currently, I'm using the default @SpringBootApplication annotation with the following properties in application.properties:

spring.datasource.url=jdbc:mysql://localhost/dbname
spring.datasource.username=X
spring.datasource.password=X
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.naming_strategy=my.package.CustomNamingStrategy

Since JPA 2.1, I should be able to use the javax.persistence.schema-generation.* properties, but setting them in my application.properties seems to have no effect.

I've seen examples like this that wire up a whole bunch of extra beans, but they aren't using Mysql. And in any case, doing it like that requires me to configure many options that spring is taking care of for me now.

My goals are to:

  • Generate a schema creation sql script in the MYSQL dialect
  • without a database connection being required
  • Output the script in the build directory
  • Also generating hibernate envers tables would be a huge plus.

I do not want to:

  • Create/drop schemas on a live database

Lib versions:

   hibernate          : 4.3.11.FINAL
   spring framework   : 4.2.5.RELEASE
   spring-boot        : 1.3.3.RELEASE
   spring-data-jpa    : 1.10.1.RELEASE   // for  querydsl 4 support
   spring-data-commons: 1.12.1.RELEASE   // for  querydsl 4 support

(Using gradle, not maven)

Hibernate Solutions


Solution 1 - Hibernate

Ah, right after I posted this question a section of the spring data docs caught my eye:

> 73.5 Configure JPA properties In addition all properties in > spring.jpa.properties.* are passed through as normal JPA properties > (with the prefix stripped) when the local EntityManagerFactory is > created.

So, to answer my own question: prefix the javax.persistence properties with spring.jpa.properties:

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

After doing this, the schema file was generated automatically in the project root.

Solution 2 - Hibernate

This is yml specific configuration for making spring boot generate ddl creation script in root folder:

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

Solution 3 - Hibernate

Updating your jpa properties will generate the scripts for you.

			<prop key="javax.persistence.schema-generation.scripts.action">drop-and-create</prop>
			<prop key="javax.persistence.schema-generation.scripts.create-target">./create_mssql.sql</prop>
			<prop key="javax.persistence.schema-generation.scripts.drop-target">./drop_mssql.sql</prop>

This will generate the scripts in the given location. There are other properties as well which can be used on various use-cases, please refer here

The whole configuration will look like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="my-persistence-unit" transaction-type="JTA">
    <description>Forge Persistence Unit</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
      
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/>
    </properties>
  </persistence-unit>
</persistence>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaseyView Question on Stackoverflow
Solution 1 - HibernateCaseyView Answer on Stackoverflow
Solution 2 - HibernatethkapasiView Answer on Stackoverflow
Solution 3 - HibernateJITHIN_PATHROSEView Answer on Stackoverflow