Spring Boot 应用程序不会为 JPA @Table 注释创建模式

And*_*kin 6 java spring hibernate jpa spring-boot

我想让表位于不同的数据库模式中。但不幸的是,我无法通过 Spring Boot 实现这一点。这是重现它的步骤。

  1. http://start.spring.io版本 2.0.5上创建一个新的 Spring Boot 项目(带有 derby 和 PostgreSQL 依赖项)

  2. 创建简单实体

@Entity
@Table(name = "my_table")
public class MyTable {
    @Id Integer id;
}
Run Code Online (Sandbox Code Playgroud)
  1. 仅将下一个属性添加到值为“更新”或“创建”的 application.properties(如果您尝试使用“create-drop”,则会在此处描述另一个错误:https : //github.com/spring-projects/spring-boot /issues/7706#issuecomment-268798059)。现在默认使用 Derby 数据源。

spring.jpa.hibernate.ddl-auto=create

  1. 运行生成的测试或主类。确保一切正常。

  2. 修改实体,schema@Table注解添加属性。现在实体看起来像:

@Entity
@Table(name = "my_table", schema = "my_schema")
public class MyTable {
    @Id Integer id;
}
Run Code Online (Sandbox Code Playgroud)
  1. 运行测试(或主类)。这次我在 Spring Boot 初始化过程中出现错误“ java.sql.SQLSyntaxErrorException: Schema 'MY_SCHEMA' does not exist”:

完整日志列表可在此处获得:https : //gist.github.com/asaushkin/8d767c92b2e7025dd359f7be43eefdd6

  1. 检查 PostgreSQL。此错误也会在 PostgreSQL 实例上重现。如果没有 'schema' 属性,Spring Boot 应用程序运行得很好,但是一旦这个属性出现在 @Table 注释上,就会抛出异常。

完整日志在这里:https : //gist.github.com/asaushkin/dd0d677964556bf943c4f013d4785372

我的问题是:为什么模式不是由 Spring Boot 创建的?

这些选项也不能解决这个问题:

spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas=true
spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true
Run Code Online (Sandbox Code Playgroud)

链接

  1. https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl
  2. https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-configure-jpa-properties

更新(2019 年 3 月 11 日):

我刚刚检查了问题的当前行为。我想知道,但目前使用 Derby 驱动程序一切正常,并且该表是使用指定的架构创建的。但是在 PostgreSQL 中,错误仍然存​​在。

生成的 SQL(用于 PostgreSQL)是:

spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas=true
spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true
Run Code Online (Sandbox Code Playgroud)

小智 0

检查您是否在 application.properties 文件中指定了数据库方言,以了解更多信息,请检查此线程。

无法让spring boot自动创建数据库模式