【问题标题】:How to generate a ddl creation script with a modern Spring Boot + Data JPA and Hibernate setup?如何使用现代 Spring Boot + Data JPA 和 Hibernate 设置生成 ddl 创建脚本?
【发布时间】:2016-08-26 06:25:27
【问题描述】:

目前,我在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脚本
  • 无需数据库连接
  • 在构建目录中输出脚本
  • 另外生成 hibernate envers 表将是一个巨大的优势。

我不想:

  • 在实时数据库上创建/删除架构

Lib 版本:

   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)

【问题讨论】:

    标签: hibernate spring-boot spring-data spring-data-jpa


    【解决方案1】:

    啊,就在我发布这个问题之后,spring 数据文档的一部分引起了我的注意:

    73.5 Configure JPA properties除此之外的所有属性 spring.jpa.properties.* 作为普通 JPA 属性传递 (去掉前缀)当本地 EntityManagerFactory 是 已创建。

    所以,回答我自己的问题:在 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
    

    完成此操作后,架构文件在项目根目录中自动生成。

    【讨论】:

    • 是否足以将这些属性添加到您的 Spring 配置中以获得您想要的结果,或者您是否还必须编写一些 Java 代码来调用诸如 Persistence.generateSchema(...) 之类的东西?如果您可以分享,希望看到更多您的解决方案。
    • 我看到文件是在运行时生成的。有没有办法“离线”生成它?
    • 有没有办法添加;在每个 ddl 命令之后?是否可以在没有 create-drop/create ddl 的情况下运行?是否可以在不运行整个应用程序的情况下运行?
    • 我想做的是将 DDL 生成为文本文件,以便更轻松地设置 Flyway 迁移的样板工作,而不是自动执行它...
    • 如果我想为现有数据库获取“update.sql”而不是为空数据库创建 DDL,该怎么办?
    【解决方案2】:

    这是用于使spring boot在根文件夹中生成ddl创建脚本的yml特定配置:

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

    【讨论】:

    • 请为您的回答提供一些信息。
    • *.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
    【解决方案3】:

    更新您的 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>
    

    【讨论】:

    • 您在问为什么它不起作用stackoverflow.com/questions/55275380/… 并发布相同的答案?
    • 我得到了答案,是财产遗漏了
    • 你能放更多结构化的代码示例吗?我将您的示例放入aplicationContext.xmlorg.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 下的jpaProperties 部分,但它什么也没做。
    • 我已经添加了整个配置文件。
    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 2021-02-13
    • 2015-11-12
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多