views:

458

answers:

2

Hi gang,

I have a pretty simple JPA @ManyToMany relationship set up in my application. A Product is a part of one or more OrderSystems. Each OrderSystem can have many Products. It's a typical many-to-many relationship.

The problem I'm fighting with is this: If I use orderSystems.remove() to remove a reference from a Product to an OrderSystem, the entry appears to be removed (within that session). However, the entry isn't removed from the cross-reference table on flush, and when I reload the Product it has the previous set of OrderSystems.

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 anyone point me at what I'm missing here?

+2  A: 

What does the code where you do the remove look like? Are you doing it in a transaction, and are you remembering to commit your changes?

MarkusQ
Yes to both -- I have figured out what I was doing wrong, and will submit my own answer to it.
Jim Kiley
+1  A: 

Turned out that the problem was that I was not clearing the relationship in both directions. I would remove a Product from the OrderSystem but not remove the OrderSystem from the Product.

Jim Kiley

related questions