我有以下的域模型錯誤刷新JPA實體
Currency ----<Price>---- Product
或英文
A Product has one or more Prices. Each Price is denominated in a particular Currency.
Price
具有其由外鍵的一個複合主鍵(由下面PricePK
代表) Currency
和Product
。在JPA註解的Java類的相關章節低於(getter和setter大多省略):
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Currency {
@Id
private Integer ix;
@Column
private String name;
@OneToMany(mappedBy = "pricePK.currency", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Price> prices = new ArrayList<Price>();
}
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "pricePK.product", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Price> defaultPrices = new ArrayList<Price>();
}
@Embeddable
public class PricePK implements Serializable {
private Product product;
private Currency currency;
@ManyToOne(optional = false)
public Product getProduct() {
return product;
}
@ManyToOne(optional = false)
public Currency getCurrency() {
return currency;
}
}
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Price {
private PricePK pricePK = new PricePK();
private BigDecimal amount;
@Column(nullable = false)
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
@EmbeddedId
public PricePK getPricePK() {
return pricePK;
}
@Transient
public Product getProduct() {
return pricePK.getProduct();
}
public void setProduct(Product product) {
pricePK.setProduct(product);
}
@Transient
public Currency getCurrency() {
return pricePK.getCurrency();
}
public void setCurrency(Currency currency) {
pricePK.setCurrency(currency);
}
}
當我嘗試refresh的Product
一個實例,我得到的StackOverflowError,所以我懷疑有某種週期(或其他錯誤)在上面的映射中,任何人都可以發現它嗎?
+1,很好地提出問題。不過,我很好奇域模型。看起來很奇怪的是,'價格'是由'產品'+'貨幣'唯一標識的,而不是(標量)值+'貨幣'。 –
感謝Matt,'Price'類中實際上有'BigDecimal amount'字段,但我在這裏省略了它,因爲它與問題無關,並且我希望儘可能縮短代碼清單 –