我的應用程式訂單和產品中有兩個物體(一對多關聯)。我的目標是在 Order 物體中建立一個單向的 OneToMany 關系(我知道這不是最好的解決方案,但這是業務需求)。資料庫模式由 Liquibase 生成并由 Hibernate 驗證。為了清楚起見,物體已被簡化。資料庫是 Postgres。
盡管模式創建正確,但 Hibernate 會拋出例外:
Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: productId in org.hibernate.mapping.Table(orders) and its related supertables and secondary tables
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:844) ~[hibernate-core-5.6.11.Final.jar:5.6.11.Final]
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:126) ~[hibernate-core-5.6.11.Final.jar:5.6.11.Final]
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1740) ~[hibernate-core-5.6.11.Final.jar:5.6.11.Final]
... 37 common frames omitted
Caused by: org.hibernate.MappingException: Unable to find column with logical name: productId in org.hibernate.mapping.Table(orders) and its related supertables and secondary tables
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:839) ~[hibernate-core-5.6.11.Final.jar:5.6.11.Final]
... 39 common frames omitted
為了證明架構是正確生成的,我用 @ManyToOne 替換了@OneToMany注釋,它作業正常!突然之間,Hibernate 能夠找到該列。之后我開始假設 Hibernate 中存在某種錯誤......
有誰知道,如何解決這個問題?
我的代碼如下所示:
訂單.java
@Entity
@Table(name = "orders")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderId;
@Column
private String clientName;
@OneToMany
@JoinColumn(name = "productIdFK", referencedColumnName = "productId")
private List<Product> productList = new ArrayList<>();
}
產品.java
@Entity
@Table(name = "products")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
@Column
private String name;
}
Liquibase 腳本
{
"databaseChangeLog": [
{
"changeSet": {
"id": "Create PRODUCT table",
"author": "me",
"changes": [
{
"createTable": {
"tableName": "products",
"columns": [
{
"column": {
"name": "productId",
"type": "bigint",
"constraints": {
"nullable": false,
"unique": true,
"primaryKey": true
}
}
},
{
"column": {
"name": "name",
"type": "varchar(250)",
"constraints": {
"nullable": true,
"unique": false
}
}
}
]
}
},
{
"createTable": {
"tableName": "orders",
"columns": [
{
"column": {
"name": "orderId",
"type": "bigint",
"constraints": {
"nullable": false,
"unique": true,
"primaryKey": true
}
}
},
{
"column": {
"name": "clientName",
"type": "varchar(250)",
"constraints": {
"nullable": true,
"unique": false
}
}
},
{
"column": {
"name": "productIdFK",
"type": "bigint",
"constraints": {
"nullable": true,
"unique": false
}
}
}
]
}
}
]
}
}
]
}
來自 liquibase 腳本的片段生成關系
{
"addForeignKeyConstraint": {
"constraintName": "fk_product_order",
"baseColumnNames": "productIdFK",
"baseTableName": "orders",
"referencedColumnNames": "productId",
"referencedTableName": "products"
}
}
uj5u.com熱心網友回復:
@OneToMany
@JoinColumn(name = "orderId", referencedColumnName = "productId")// try orderId
private List<Product> productList = new ArrayList<>();
uj5u.com熱心網友回復:
一旦你嘗試這個希望這會成功
在Order.java中更改為這個
@OneToMany(cascade = CascadeType.ALL, mappedBy= "order")
private List<Product> productList = new ArrayList<>();
在Product.java中添加這個
@ManyToOne
@JsonIgnore
private Order order;
讓我知道
uj5u.com熱心網友回復:
你也應該試試這個。
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order")
private List<Product> productList = new ArrayList<>();
連同這個在 Product.java
@ManyToOne
@JoinColumn(name = "productId",
foreignKey = @ForeignKey(
name = "product_id_fk"))
private Order order;
uj5u.com熱心網友回復:
首先,讓我們澄清一下 1 是什么以及您的架構中有多少。由于 Order 參考了 Product,這意味著許多 Orders 可能與同一個 Product 相關,不是嗎?通常首選雙向關系,但您需要單向關系,它應該類似于
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderId;
@Column
private String clientName;
}
和
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long productId;
@Column
private String name;
@OneToMany
@JoinColumn(name = "productIdFK", referencedColumnName = "productId")
private List<Order> orderList = new ArrayList<>();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515507.html
