0
可以說我有可以在保存之前獲取級聯的ManyToOne關係以查找子實體嗎?
@Entity
@Table(name = "currency", uniqueConstraints = {})
public class Currency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CURRENCY_ID")
private Integer currencyId;
@NaturalId
@Column(name = "CURRENCY_CODE")
private String code;
}
@Entity
@Table(name = "price", uniqueConstraints ={})
public class Price{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PRICE_ID")
private Integer priceId;
@Column(name= "AMOUNT")
private Double amount;
@ManyToOne(fetch = FetchType.EAGER)
@Cascade({CascadeType.ALL})
@JoinColumn(name = "CURRENCY_ID", nullable = false)
private Currency currency;
}
而且可以說我已經有一個貨幣英鎊的貨幣代碼的貨幣表。
,然後我想做到以下幾點
Price p = new Price();
p.setAmount(3.5);
p.setCurrency(new Currency("GBP"));
sessionFactory.getCurrentSession().save(p)
目前,我將獲得:
Caused by: org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; UK_R07G412IP03G7P3N6P5O2PARG table: CURRENCY
是否有可能以這樣的方式配置Hibernate,它會嘗試來查找在試圖級聯保存操作之前,代碼爲GBP的貨幣,並且如果代碼爲GBP的貨幣已經存在,它只會在價格對象上設置該貨幣的ID?