简体   繁体   English

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

[英]Spring Boot application doesn't create schemas for JPA @Table annotation

I want to have tables located in different database schemas.我想让表位于不同的数据库模式中。 But unfortunately, I can't achieve this with Spring Boot.但不幸的是,我无法通过 Spring Boot 来实现这一点。 Here steps to reproduce it.这里是重现它的步骤。

  1. Create a new Spring Boot project on http://start.spring.io version 2.0.5 (with derby and PostgreSQL dependencies) Create a new Spring Boot project on http://start.spring.io version 2.0.5 (with derby and PostgreSQL dependencies)

  2. Create simple entity创建简单实体

@Entity
@Table(name = "my_table")
public class MyTable {
    @Id Integer id;
}
  1. Add only next property to the application.properties with value 'update' or 'create' (if you try 'create-drop' then you get another error described here: https://github.com/spring-projects/spring-boot/issues/7706#issuecomment-268798059 ).仅将值为“update”或“create”的下一个属性添加到 application.properties(如果您尝试“create-drop”,则会收到此处描述的另一个错误: https://github.com/spring-projects/spring-boot /issues/7706#issuecomment-268798059 )。 Now Derby datasource will be used by default.现在将默认使用 Derby 数据源。

spring.jpa.hibernate.ddl-auto=create

  1. Run a generated test or main class.运行生成的测试或主 class。 Be sure all works fine.确保一切正常。

  2. Modify the entity, add attribute schema to the @Table annotation.修改实体,将属性schema添加到@Table注释。 Now the entity looks like:现在实体看起来像:

@Entity
@Table(name = "my_table", schema = "my_schema")
public class MyTable {
    @Id Integer id;
}
  1. Run a test (or main class).运行测试(或主类)。 This time I get an error while Spring Boot initialization process " java.sql.SQLSyntaxErrorException: Schema 'MY_SCHEMA' does not exist ":这次我收到一个错误,而 Spring 启动初始化过程“ java.sql.SQLSyntaxErrorException: Schema 'MY_SCHEMA' does not exist ”:

Full log listing is available here: https://gist.github.com/asaushkin/8d767c92b2e7025dd359f7be43eefdd6完整的日志列表可在此处获得: https://gist.github.com/asaushkin/8d767c92b2e7025dd359f7be43eefdd6

  1. Check on PostgreSQL.检查 PostgreSQL。 This error reproduces on a PostgreSQL instance too.此错误也会在 PostgreSQL 实例上重现。 Without the 'schema' attribute Spring Boot app runs perfect, but as soon as this attribute appears on the @Table annotation the exceptions are thrown.如果没有“模式”属性 Spring 启动应用程序运行完美,但只要此属性出现在 @Table 注释上,就会引发异常。

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

My question is: why are schemas not created by Spring Boot?我的问题是:为什么 Spring Boot 没有创建模式?

These options can't resolve this issue too:这些选项也无法解决此问题:

spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas=true
spring.jpa.properties.hibernate.hbm2dll.create_namespaces=true

Links链接

  1. https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl 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 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-configure-jpa-properties

Update (11 March 2019):更新(2019 年 3 月 11 日):

I've just check the current behavior of the issue.我刚刚检查了问题的当前行为。 I wonder, but currently with Derby driver all works fine and the table is created with the specified schema.我想知道,但目前使用 Derby 驱动程序一切正常,并且表是使用指定的模式创建的。 But in PostgreSQL an error continues exists.但是在 PostgreSQL 中仍然存在错误。

Generated SQL (for PostgreSQL) is:生成的 SQL(对于 PostgreSQL)是:

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

Check that are you specifying the database dialect in the application.properties file or not for more check this thread.检查您是否在 application.properties 文件中指定了数据库方言以获取更多信息,请检查此线程。

Unable to get spring boot to automatically create database schema 无法让 spring boot 自动创建数据库架构

I had the same problem with PostgreSQL and JPA (ERROR ohejdbc.spi.SqlExceptionHelper - ERROR: relation "schema.table" does not exist) and I figured out this solution.我在 PostgreSQL 和 JPA 上遇到了同样的问题(错误 ohejdbc.spi.SqlExceptionHelper - 错误:关系“schema.table”不存在),我想出了这个解决方案。

In your entities classes, add escape characters \\", between database element´s name. For instance:在您的实体类中,在数据库元素的名称之间添加转义字符 \\"。例如:

Use this form:使用这个表格:

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

Rather than a typical way而不是典型的方式

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

The same applies for columns names这同样适用于列名

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

Rather than而不是

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

UPDATE:更新:

I discovered the reason that was causing the problem.我发现了导致问题的原因。 I used Valentina Studio to create my DB, if I use capital letters (MYTABLE), instead lower-case letters (mytable) to create my tables, I had to use double quotes inside SQL statements.我使用 Valentina Studio 创建我的数据库,如果我使用大写字母 (MYTABLE) 而不是小写字母 (mytable) 来创建我的表,我必须在 SQL 语句中使用双引号。 This is because PostgreSQL is case sensitive.这是因为 PostgreSQL 区分大小写。 If you can´t change your database then use my last solution.如果您无法更改数据库,请使用我的最后一个解决方案。 Also is a good idea to enable spring.jpa.show-sql=true property, so you can see hibernate´s queries and know what´s going on.启用 spring.jpa.show-sql=true 属性也是一个好主意,这样您就可以查看 hibernate 的查询并了解发生了什么。

Rename spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas to spring.jpa.properties.javax.persistence.create-database-schemas . Rename spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas to spring.jpa.properties.javax.persistence.create-database-schemas . In other words, remove '.schema-generation'.换句话说,删除“.schema-generation”。

I just had the same problem not with PostgreSQL but H2 - schemas weren't being created.我只是遇到了同样的问题,不是 PostgreSQL 而是 H2 - 没有创建模式。 But, as I've discovered, the problem is not with H2 (or, likely, PostgreSQL) but, rather, Hibernate (it deviates from the standard, regarding that nomenclature).但是,正如我发现的那样,问题不在于 H2(或者,可能是 PostgreSQL),而在于 Hibernate(关于该命名法,它偏离了标准)。 That likely means that this solution will work for you too.这可能意味着该解决方案也适用于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM