I have the following domain model
Currency ----< Price >---- Product
Or in English
The product has one or more prices. Each price is expressed in a specific currency.
Pricehas a composite primary key (indicated below PricePK), which consists of foreign keys Currencyand Product. The relevant sections of JPA-annotated Java classes are below (getters and seters are mostly omitted):
@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);
}
}
When I try to refresh the instance Product, I get a StackOverflowError, so I suspect there is some kind of loop (or other error) in the above display, can anyone define it?
Dónal