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

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

问题描述

目前,我在 application.properties 中使用默认的 @SpringBootApplication 注释和以下属性:

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

从 JPA 2.1 开始,我应该可以使用 javax.persistence.schema-generation.* 属性,但是在我的 application.properties 中设置它们似乎没有效果.

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.

我见过一些示例 像这样,它们连接了一大堆额外的 bean,但是他们没有使用Mysql.无论如何,这样做需要我配置 spring 现在为我处理的许多选项.

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.

我的目标是:

  • 用MYSQL方言生成模式创建sql脚本
  • 无需数据库连接
  • 输出构建目录下的脚本
  • 同时生成休眠环境表将是一个巨大的优势.

我不想:

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

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)

(Using gradle, not maven)

推荐答案

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

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

73.5 配置 JPA 属性 另外所有属性在spring.jpa.properties.* 作为普通 JPA 属性传递(去掉前缀)当本地 EntityManagerFactory 是已创建.

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:

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天全站免登陆