簡體   English   中英

休眠刪除多對多關聯

[英]hibernate delete many-to-many association

我正在上商店課。

@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);
    }

和產品類別

@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>();

和類StoreProductLink,這是聯接表

@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)

在做

store.removeProduct(product);

此函數從Set命名產品中刪除該產品示例。

t1 = entityManager.merge(t);

返回具有產品集但沒有產品示例的商店。

updateHibernateFunction(store);

但是,StoreProductLink表仍具有該記錄。

添加操作正常。 刪除操作無效。 :/

添加

這是更新功能

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;
}

這不是級聯的問題,因為級聯的意思是(在DELETE情況下):“刪除目標時刪除目標”。

嘗試orphanRemoval

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM