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

时间:2017-07-14 21:11:41

标签: hibernate jpa hibernate-mapping

我在查找表中使用复合键有以下情况:

LU_MASTER_TABLE
  - [PK1] TYPE =正在查找的值的类型
  - [PK2] ID =要应用于该表的查找值的id
  - DESC = ID的说明。

TABLE2>
  - TYPE_A_ID
  - TYPE_B_ID

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

1 个答案:

答案 0 :(得分: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;

}