Why does my remove () not actually remove the link from the database using JPA @ Many-to-Many

I have a pretty simple JPA @ManyToMany connection installed in my application. A Product is part of one or more OrderSystem s. Each OrderSystem can have many Product s. This is a typical many-to-many relationship.

The problem I'm fighting is this: if I use orderSystems.remove() to remove the link from Product to OrderSystem , the record will be deleted (during this session). However, the record is not deleted from the flash cross-reference table, and when I reload Product , it has the previous set of OrderSystem s.

I have the following code:

 @Entity @Table(name = "p_product_versions") @Validation public class Product { private List<OrderSystem> orderSystems; @ManyToMany @JoinTable(name = "p_order_systems_has_product_versions", joinColumns = @JoinColumn(name = "p_product_versions_prod_version_id"), inverseJoinColumns = @JoinColumn(name = "p_order_systems_system_id")) public List<OrderSystem> getOrderSystems() { return orderSystems; } } @Entity @Table(name = "p_order_systems") @Validation public class OrderSystem { private List<Product> products; @ManyToMany (mappedBy = "orderSystems") public List<Product> getProducts() { return products; } } 

Can someone point me to what I am missing here?

+4
source share
3 answers

It turned out that the problem is that I did not clear the relationship in both directions. I would remove the product from OrderSystem, but I would not remove OrderSystem from the Product.

+2
source

What does the code in which you do the removal look like? Do you do this in a transaction and do you remember that you want to make changes?

+2
source

You need to remove the relationship on both sides. The way to do this is:

 // Find your 2 beans with entityManager product.setOrderSystems(null); orderSystem.setProducts(null); // then persist() and flush() 
+1
source

All Articles