我有以下的域模型错误刷新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'字段,但我在这里省略了它,因为它与问题无关,并且我希望尽可能缩短代码清单 –