2017-07-14 79 views
0

我曾與一個查找表中的複合鍵下面的情況,模型查找表複合鍵:如何使用JPA

LU_MASTER_TABLE
- [PK1] TYPE =類型的值進行查找
- [PK2] ID =要查找的值適用於該表的編號
- DESC =編號的說明。

TABLE2>
- TYPE_A_ID
- TYPE_B_ID

什麼是創建一個使用Spring JPA一對一的關係的最好方法?

回答

0

到目前爲止,下面是我能想到的最好的。儘管它在工作時不使用組合鍵,但它並不真正代表數據庫結構。

@Entity 
@Table(name= "LU_MASTER_TABLE") 
public class LuMasterTable { 

    @Id 
    @Column(name = "ID") 
    protected String id; 

    // Note I did not make typeField a PK even though it is in DB. 
    // Ideally it would be part of composite key, 
    // though I wasn't able to get composite key to work given 
    // the scenario here where upon joining with another table, 
    // it must be a type of "constant". See where clause in other 
    // table below to see what I mean. 
    @Column(name = "TYPE") 
    protected String typeField; 

} 

表2

@Entity 
@Table(name = "TABLE2") 
public class Table2{ 

... 

    @OneToOne 
    @JoinColumn(name="TYPE_A_ID") 
    @Where(clause = "typeField = TYPE_A") 
    protected LuMasterTable typeALuMasterTable; 

    @OneToOne 
    @JoinColumn(name="TYPE_B_ID") 
    @Where(clause = "typeField = TYPE_B") 
    protected LuMasterTable typeBLuMasterTable; 

}