如何在JPA引用的表列上使用uniqueConstraint

时间:2017-07-21 00:11:19

标签: java spring jpa

如何在唯一约束中包含引用的表列?我想为列toEmailreceipt.receiptSeriesreceipt.receiptNum的表调查创建唯一约束。

请参阅以下实体:

收据

@Entity
@Table(name = "RECEIPT")
public class Receipt {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ID")
  private Long id;

  @Column(name = "RECEIPT_SERIES")
  private String receiptSeries;

  @Column(name = "RECEIPT_NUM")
  private String receiptNum;
}

调查

@Entity
@Table(name = "SURVEY"}
public class Survey {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ID")
  private Long id;

  @ManyToOne(optional = false)
  @JoinColumn(name = "RECEIPT_ID")
  private Receipt receipt;

  @Column(name = "TO_EMAIL")
  private String toEmail;
}

我尝试过使用以下方法但没有成功:

@Table(name = "SURVEY", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"TO_EMAIL", "RECEIPT.RECEIPT_SERIES", "RECEIPT.RECEIPT_NUM"})})

使用带有receiptSeriesreceiptNum的复合主键作为收据不是一种选择。

1 个答案:

答案 0 :(得分:0)

尝试类似的方法

@Table(name="notification_status", uniqueConstraints= 
       arrayOf(UniqueConstraint(columnNames= arrayOf("external_user_id",
                                                     "external_organization_id",  
                                                     "notification_id"))))