tengo el siguiente modelo de dominioerror refrescante APP Entidad
Currency ----<Price>---- Product
O en Inglés
A Product has one or more Prices. Each Price is denominated in a particular Currency.
Price
tiene una clave principal compuesta (representar por PricePK
abajo) que se compone de las claves ajenas a Currency
y Product
. Las secciones pertinentes de las clases Java anotadas-APP están por debajo (captadores y definidores mayoría omitidas):
@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);
}
}
Cuando intento refresh una instancia de Product
, aparece un StackOverflowError, por lo que sospechan que hay algún tipo de ciclo (u otro error) en el mapeo anterior, ¿alguien puede detectarlo?
+1, muy bien plantea la pregunta. Tengo curiosidad acerca del modelo de dominio, sin embargo. Parece extraño que un 'Precio' se identifique de forma única por' Producto' + 'Moneda' en lugar de, digamos, su valor (escalar) +' Moneda'. –
Gracias Matt, en realidad hay un campo 'cantidad BigDecimal' en la clase' Price' también, pero lo omití aquí porque no es relevante para la pregunta, y quería mantener la lista del código lo más corta posible –