2017-07-14 64 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; 

}