gpt4 book ai didi

java - JPA 中复合键的唯一约束

转载 作者:行者123 更新时间:2023-11-30 02:37:28 25 4
gpt4 key购买 nike

我有一个带有两个外键的实体,我也想将其用作约束JPA 有没有办法定义一个约束,该约束只有在以下情况下才是唯一的:重复相同的 FROM_CURRENCY_IDTO_CURRENCY_ID,但允许 if 值FROM_CURRENCY_IDTO_CURRENCY_ID 的反转

@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

}

最佳答案

@Table注释添加唯一约束定义

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

关于java - JPA 中复合键的唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com