2017-03-09 165 views
1

我有兩個外鍵,我也想爲約束 使用一個實體存在的JPA任何方式來定義的約束只會是唯一的,如果 相同FROM_CURRENCY_IDTO_CURRENCY_ID重複,但允許值是否爲 反轉FROM_CURRENCY_IDTO_CURRENCY_ID唯一約束複合主鍵在JPA

@Entity 
@Table(name="CURRENCY_CONVERSION") 
public class CurrencyConversion extends BaseEntity { 

    private static final long serialVersionUID = -2440725717151990999L; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "FROM_CURRENCY_ID") 
    private Currency fromCurrencyId; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "TO_CURRENCY_ID") 
    private Currency toCurrencyId; 

    @Column(name = "CURRENCY_RATE", nullable = false) 
    private Double rate; 
    // getter setters 

} 

回答

2

添加唯一約束定義@Table註釋

@Table(
    uniqueConstraints= 
     @UniqueConstraint(columnNames={"column_1", "column_2"}) 
)