I have an app with a many-to-many relationship between entities (Product, Order) , the intermediary table is ProductOrder
the first 2-3 times I lauched the app it worked fine, but now I get
java.lang.StackOverflowError
The DB was updated on the first runs (table for ProductOrder was populated with the correct data).
Here is the code for Product, Order, ProductOrder (id and other fields excluded) :
Product code:
Order code:
ProductOrder code:
ProductOrderId code:
The stack trace changes a bit with every launch , but the main error is still java.lang.StackOverflowError , full stack trace is availale at StackOverflowError - f9f52aee
Code:
for(Item item : items)
{
ProductOrder po = new ProductOrder();
po.setOrder(order);
po.setProduct(item.getProduct());
po.setQuantity(item.getQuantity());
item.getProduct().getProductOrders().add(po); // FetchType is set to EAGER
productService.save(item.getProduct());
}
java.lang.StackOverflowError
The DB was updated on the first runs (table for ProductOrder was populated with the correct data).
Here is the code for Product, Order, ProductOrder (id and other fields excluded) :
Product code:
Code:
@Entity
@Table(name = "product")
public class Product implements Serializable{
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.product", cascade = CascadeType.ALL)
private Set<ProductOrder> productOrders = new HashSet<ProductOrder>();
...
}
Code:
@Entity
@Table(name = "orders")
public class Order implements Serializable{
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.order")
private Set<ProductOrder> productOrders = new HashSet<ProductOrder>();
}
Code:
@Entity
@Table(name = "product_order")
@AssociationOverrides({
@AssociationOverride(name = "pk.product", joinColumns = @JoinColumn(name = "id_product")),
@AssociationOverride(name = "pk.order", joinColumns = @JoinColumn(name = "id_order")) })
public class ProductOrder implements Serializable{
private ProductOrderId pk = new ProductOrderId();
...
@EmbeddedId
public ProductOrderId getPk() {
return pk;
}
}
Code:
@Embeddable
public class ProductOrderId implements Serializable{
@ManyToOne
public Produs getProdus() {
return produs;
}
@ManyToOne
public Order getOrder() {
return order;
}
}