如何使用现代 Spring Boot + Data JPA 和 Hibernate 设置生成 ddl 创建脚本?
How to generate a ddl creation script with a modern Spring Boot + Data JPA and Hibernate setup?
目前,我在 application.properties
中使用具有以下属性的默认 @SpringBootApplication
注释:
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
从 JPA 2.1 开始,我应该可以使用 javax.persistence.schema-generation.*
属性,但是在我的 application.properties 中设置它们似乎没有效果。
我看过示例 like this 连接了一大堆额外的 bean,但它们没有使用 Mysql。无论如何,这样做需要我配置许多 spring 现在正在为我处理的选项。
我的目标是:
- 在 MYSQL 方言中生成模式创建 sql 脚本
- 不需要数据库连接
- 在build目录下输出脚本
- 同时生成 hibernate envers 表将是一个巨大的优势。
我不想:
- Create/drop 实时数据库上的模式
库版本:
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
(使用 gradle,不是 maven)
啊,在我发布这个问题后,spring 数据文档的一部分引起了我的注意:
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.
因此,回答我自己的问题:在 javax.persistence 属性前加上 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
执行此操作后,在项目根目录中自动生成了架构文件。
这是 yml 特定配置,用于使 spring 引导在根文件夹中生成 ddl 创建脚本:
spring:
jpa:
properties:
javax:
persistence:
schema-generation:
create-source: metadata
scripts:
action: create
create-target: create.sql
更新您的 jpa properties
将为您生成脚本。
<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>
这将在给定位置生成脚本。还有其他属性可用于各种用例,请参考here
整个配置如下所示
<?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>
目前,我在 application.properties
中使用具有以下属性的默认 @SpringBootApplication
注释:
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
从 JPA 2.1 开始,我应该可以使用 javax.persistence.schema-generation.*
属性,但是在我的 application.properties 中设置它们似乎没有效果。
我看过示例 like this 连接了一大堆额外的 bean,但它们没有使用 Mysql。无论如何,这样做需要我配置许多 spring 现在正在为我处理的选项。
我的目标是:
- 在 MYSQL 方言中生成模式创建 sql 脚本
- 不需要数据库连接
- 在build目录下输出脚本
- 同时生成 hibernate envers 表将是一个巨大的优势。
我不想:
- Create/drop 实时数据库上的模式
库版本:
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
(使用 gradle,不是 maven)
啊,在我发布这个问题后,spring 数据文档的一部分引起了我的注意:
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.
因此,回答我自己的问题:在 javax.persistence 属性前加上 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
执行此操作后,在项目根目录中自动生成了架构文件。
这是 yml 特定配置,用于使 spring 引导在根文件夹中生成 ddl 创建脚本:
spring:
jpa:
properties:
javax:
persistence:
schema-generation:
create-source: metadata
scripts:
action: create
create-target: create.sql
更新您的 jpa properties
将为您生成脚本。
<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>
这将在给定位置生成脚本。还有其他属性可用于各种用例,请参考here
整个配置如下所示
<?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>