hibernate - 如何使用现代 Spring Boot + Data JPA 和 Hibernate 设置生成 ddl 创建脚本?

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

目前,我在 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 表将是一个巨大的优势。

我不想:

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

库版本:

   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 Data 文档的一部分引起了我的注意:

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

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

关于hibernate - 如何使用现代 Spring Boot + Data JPA 和 Hibernate 设置生成 ddl 创建脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36966337/

相关文章:

hibernate - 使用 Hibernate 和 EhCache 缓存集合时出现问题

java - 如何在使用大量记录时使用 Hibernate 更快地进行更新

Spring Feign 客户端调用在应该停留在 try block 时进入异常 block

java - Spring Boot 通过 REST 端点将大型数据库导出到 csv

java - Spring-data-elasticsearch 元数据_score

Spring DATA JPA Between 和 IsBetween 关键字

java - 与 hibernate 注解的接口(interface)

java - Spring MVC @ModelAttribute 方法返回 "Bad request"400

java - NGINX反向代理背后的SpringBoot API REST

java - 如何根据 HTTP header 值在 RestController 之间切换?