假設我有一個表Customer,有這些欄位
- customer_id (PK)
- 客戶名稱
- 客戶的姓氏
- table1_id (PK)
- table1_col1
- table1_customer_id (FK參考
Customer中customer_id) - table2_id (PK)/li>
- table2_col1
- table2_customer_id (FK參考
Customer中customer_id)
我還有一些其他的表,這些表有一個FK指向一些客戶。例如,Table1有這些欄位:
Table2有這些欄位:
在Customer表中參考客戶的表的串列可能會增加,這里是我在JPA映射中掙扎的地方。
我有一個物體Customer,像這樣:
@Entity
@Table(name = "customers")
public class Customer {
@Id
@Column(name = "customer_id")
private String customerId;
@Column(name = "customer_name")
private String customerName;
@Column(name = "customer_surname")
private String customerSurname;
// getters & setters
}
但是現在我不知道如何映射其他表的FKs。例如,我將如何映射Table1?
@Entity
@Table(name = "table1)
public class Table1 {
@Id
@Column(name = "table1_id")
private Long table1Id;
@Column(name = "table1_col1)
private String table1Col1;
//private Long table1CustomerId; 我怎么能把這個變成FK呢?
//getters & setters
}
約束條件是每個表(Table1, ..., TableN)最多可以有一個客戶,但是每個客戶可以在Table1, ..., TableN中的任何一個。
謝謝你
uj5u.com熱心網友回復:
使用這個:
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "table1_customer_id")
private Customer customer。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327144.html
標籤:
