【问题标题】:Spring Boot application doesn't create schemas for JPA @Table annotationSpring Boot 应用程序不会为 JPA @Table 注释创建模式
【发布时间】:2019-02-17 03:45:04
【问题描述】:

我想让表位于不同的数据库架构中。但不幸的是,我无法使用 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;
}
  1. 仅将值为“update”或“create”的下一个属性添加到 application.properties(如果您尝试“create-drop”,则会收到此处描述的另一个错误:https://github.com/spring-projects/spring-boot/issues/7706#issuecomment-268798059)。现在将默认使用 Derby 数据源。

spring.jpa.hibernate.ddl-auto=create

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

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

@Entity
@Table(name = "my_table", schema = "my_schema")
public class MyTable {
    @Id Integer id;
}
  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

链接

  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)是:

create table my_schema.my_table (id int4 not null, primary key (id))

【问题讨论】:

标签: java spring hibernate spring-boot jpa


【解决方案1】:

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

Unable to get spring boot to automatically create database schema

【讨论】:

    【解决方案2】:

    我在 PostgreSQL 和 JPA 上遇到了同样的问题(错误 o.h.e.jdbc.spi.SqlExceptionHelper - 错误:关系“schema.table”不存在),我想出了这个解决方案。

    在您的实体类中,在数据库元素名称之间添加转义字符 \"。例如:

    使用这个表格:

    @Table(name = "\"USUARIO\"", schema="\"INVENTARIODB\"")
    

    而不是典型的方式

    @Table(name = "USUARIO", schema="INVENTARIODB")
    

    列名也是如此

    @Column(name = "\"ID\"", nullable = false, updatable = false)    
    private Long id;
    

    而不是

    @Column(name = "ID", nullable = false, updatable = false)    
    private Long id;
    

    更新:

    我发现了导致问题的原因。我使用 Valentina Studio 创建我的数据库,如果我使用大写字母 (MYTABLE) 而不是小写字母 (mytable) 来创建我的表,我必须在 SQL 语句中使用双引号。这是因为 PostgreSQL 区分大小写。如果您无法更改数据库,请使用我的最后一个解决方案。启用 spring.jpa.show-sql=true 属性也是一个好主意,这样您就可以看到 hibernate 的查询并知道发生了什么。

    【讨论】:

    • 我刚刚检查了问题的当前行为。我想知道,但目前使用 Derby 驱动程序一切正常,并且表是使用指定的模式创建的。但是在 PostgreSQL 中仍然存在错误。生成的 SQL 为:create table my_schema.my_table (id int4 not null, primary key (id))
    猜你喜欢
    • 2020-02-17
    • 2019-03-04
    • 1970-01-01
    • 2019-03-31
    • 2020-12-21
    • 2019-11-22
    • 2019-06-15
    • 2021-09-15
    • 2016-10-24
    相关资源
    最近更新 更多