如何用现代的Spring Boot + Data JPA和Hibernate设置生成ddl创建脚本? [英] How to generate a ddl creation script with a modern Spring Boot + Data JPA and Hibernate setup?

查看:181
本文介绍了如何用现代的Spring Boot + Data JPA和Hibernate设置生成ddl创建脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在 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-代码。* 属性,但在我的application.properties中设置它们似乎没有任何作用。



我见过一些例子像这样连接了一大堆额外的bean,但他们没有使用Mysql。在任何情况下,这样做都需要我配置许多选项,现在春天正在为我照顾。



我的目标是:




  • 在不需要数据库连接的情况下,以MYSQL方言生成模式创建SQL脚本


  • 在构建目录中输出脚本

  • 同时生成hibernate envers表将会是一个巨大的优势。



我不想:


  • 在实时数据库上创建/删除模式



Lib版本:

  hibernate:4.3.11.FINAL 
spring框架:4.2.5.RELEASE
spring-boot:1.3.3.RELEASE
spring-data-jpa:1.10.1.RELEASE //对于querydsl 4支持
spring-data-commons:1.12.1.RELEASE //对于querydsl 4支持

(使用gradle,不是maven)

解决方案

在我发布这个问题后,一段春季数据文档引起了我的注意:


73.5配置JPA属性另外,所有属性在
中,当本地EntityManagerFactory被
创建时,spring.jpa.properties。*作为普通的JPA属性
(带有剥离前缀)传递。


blockquote>

所以,回答我自己的问题:用spring.jpa.properties前缀javax.persistence属性:

  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

完成此操作后,模式文件属于属自动在项目根目录下。


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)

解决方案

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.

这篇关于如何用现代的Spring Boot + Data JPA和Hibernate设置生成ddl创建脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆