如何使用 JPA 使用复合键对查找 Table 建模

How to Model a Lookup Table with Composite Key using JPA

我在查找 table 时遇到以下情况:

LU_MASTER_TABLE
- [PK1] TYPE = 正在查找的值的类型
- [PK2] ID = 应用于 table
的查找值的 ID - DESC = id 的描述。

表 2>
- TYPE_A_ID
- TYPE_B_ID

使用 Spring JPA 创建一对一关系的最佳方法是什么?

到目前为止,以下是我能想到的最好的。虽然它不使用复合键,所以虽然它可以工作,但它并不真正代表数据库结构。

 @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;

}