I have two classes that are related as follows:
Code:
class Cart {
private Long id;
private Set<Item> items;
@ManyToMany(targetEntity = Cart.class, fetch = FetchType.EAGER)
@JoinTable(
name="cart_item",
joinColumns = @JoinColumn( name="id"),
inverseJoinColumns = @JoinColumn( name="id")
)
public Set<Item> getItems() {
return items;
}
}
class Item {
private Long id;
}
If I delete an Item from the cart or delete the Cart entirely, both the Cart entity and the association in "cart_item" is removed.
However, if I delete attempt to delete an "Item" instance, I get a failed constraint exception because Hibernate is not deleting any associated entries in the "cart_item" table.
If I delete an "Item", I would expect it to remove items from the "cart_item" table. How can I make this happen?
I've tried adding an association on Item like the following:
Code:
class Item {
private Long id;
private Set<Cart> carts;
@ManyToMany(mappedBy="items")
public Set<Cart)> getCarts() {
return carts;
}
}
but that gave me the same error.
I tried adding CascadeType.REMOVE to either side of the association but it ended up removing the Cart, "cart_item" and Item records which is not what I want.
Thanks