簡體   English   中英

Spring 引導應用程序不會為 JPA @Table 注釋創建模式

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

我想讓表位於不同的數據庫模式中。 但不幸的是,我無法通過 Spring Boot 來實現這一點。 這里是重現它的步驟。

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

  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. 運行生成的測試或主 class。 確保一切正常。

  2. 修改實體,將屬性schema添加到@Table注釋。 現在實體看起來像:

@Entity
@Table(name = "my_table", schema = "my_schema")
public class MyTable {
    @Id Integer id;
}
  1. 運行測試(或主類)。 這次我收到一個錯誤,而 Spring 啟動初始化過程“ java.sql.SQLSyntaxErrorException: Schema 'MY_SCHEMA' does not exist ”:

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

  1. 檢查 PostgreSQL。 此錯誤也會在 PostgreSQL 實例上重現。 如果沒有“模式”屬性 Spring 啟動應用程序運行完美,但只要此屬性出現在 @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))

檢查您是否在 application.properties 文件中指定了數據庫方言以獲取更多信息,請檢查此線程。

無法讓 spring boot 自動創建數據庫架構

我在 PostgreSQL 和 JPA 上遇到了同樣的問題(錯誤 ohejdbc.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 的查詢並了解發生了什么。

Rename spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas to spring.jpa.properties.javax.persistence.create-database-schemas . 換句話說,刪除“.schema-generation”。

我只是遇到了同樣的問題,不是 PostgreSQL 而是 H2 - 沒有創建模式。 但是,正如我發現的那樣,問題不在於 H2(或者,可能是 PostgreSQL),而在於 Hibernate(關於該命名法,它偏離了標准)。 這可能意味着該解決方案也適用於您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM