简体   繁体   中英

hibernate delete many-to-many association

I am having a Store class.

@Entity
@Table(name = "Store")
public class Store {

    @Id
    @Column(name = "ST_Name", nullable = false)
    private String name;

        ...

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="store")
    private Set<StoreProductLink> products = new HashSet<StoreProductLink>();

    public void addProduct(Product pr, int quantity, String lt) {
        StoreProductLink link = new StoreProductLink();

        ...

        this.products.add(link);
        pr.getStores().add(link);
    }

    public void removeProduct(Product pr) {
        StoreProductLink link = new StoreProductLink();

        ....

        this.products.remove(link);
        pr.getStores().remove(link);
    }

and a Product class

@Entity
@IdClass(ProductPK.class)
@Table(name = "Product")
public class Product {
    @Id
    @Column(name = "PR_Name", nullable = false, length = 45)
    private String name;

    @Id
    @Column(name = "PR_SerialNumber", nullable = false, length = 45)
    private String serialNumber;

    ....

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="product")
    private Set<StoreProductLink> stores = new HashSet<StoreProductLink>();

and the class StoreProductLink which is the join table

@Entity
@IdClass(StoreProductLinkPK.class)
@Table(name = "StoreProductLink")
public class StoreProductLink {

    @Id
    private String storeName;

    @Id
    private String productName;

    @Id
    private String productSerialNumber;

    @Column(name = "quantity")
    private int quantity;

    @Column(name = "lt")
    private String lt;

    @ManyToOne
    @JoinColumn(name = "storeName", updatable = false, insertable = false, referencedColumnName = "ST_Name")
    private Store store;

    @ManyToOne
    @JoinColumns(value = {
            @JoinColumn(name = "productName", updatable = false, insertable = false, referencedColumnName = "PR_Name"),
            @JoinColumn(name = "productSerialNumber", updatable = false, insertable = false, referencedColumnName = "PR_Serialnumber") })
    private Product product;

.

Store store = new Store();
Product product = new Product();
product.setName(example);
...
store.addProduct(product, 5, "com");
updateHibernateFunction(store); (this inserts in StoreProductLink table this --> storeName,example, exampleSerialNumber,5,com)

Doing

store.removeProduct(product);

this function removes the product example from the Set named products. And the

t1 = entityManager.merge(t);

returns a store having a products Set without the product example.

updateHibernateFunction(store);

However the StoreProductLink table is still having the record.

Add operation works fine. Delete operation doesn't work. :/

ADDED

This is the update function

public T update(T t) 
    {
        EntityManager entityManager = DataLayer.getEntityManager();
        T t1 = null;

    EntityTransaction tx = null;

    try
    {
        tx = entityManager.getTransaction();
        tx.begin();
        t1 = entityManager.merge(t);
        tx.commit();
    }catch(RuntimeException e)
    {
        if(tx != null && tx.isActive()) tx.rollback();
        throw e;
    }finally
    {
        entityManager.close();
    }

    return t1;
}

This is not a matter of cascade, because cascade means (in the DELETE case): "delete the target when you delete me".

Try the orphanRemoval :

public class Product {
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="product",
        orphanRemoval=true)
    private Set<StoreProductLink> stores = new HashSet<StoreProductLink>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM