简体   繁体   English

Hibernate / JPA多对多

[英]Hibernate/jpa many-to-many

I'm having 2 tables Pizza and toppings, 我有2桌披萨和浇头,

   class pizza{
...
    @ManyToMany(targetEntity = com.pizzastudyds.db.bean.Topping.class, fetch = FetchType.EAGER)
        @JoinTable(name = "PIZZATOPPING", joinColumns = @JoinColumn(name = "PIZZAID"), inverseJoinColumns = @JoinColumn(name = "TOPPINGID"))
        public Set<Topping> getToppings() {
            return toppings;
        }
...
    } 

class toppings{
@ManyToMany(mappedBy = "toppings", targetEntity = com.pizzastudyds.db.bean.Pizza.class)
    public Set<Pizza> getPizzas() {
        return pizzas;
    }
}

This 2 classes have many to many relation ship , when I define toppings for an pizza. 当我定义披萨的浇头时,这两个类具有多对多关系。 Then try to delete the pizza I'm able to do so but when I'm deleting the topping object and if there is a relationship between tooping and pizza. 然后尝试删除比萨饼,但我要删除的是顶部对象,并且在漏斗和比萨饼之间是否存在关系时,可以这样做。 then I'm not able to delete the topping and get the exception of constraint viloation. 那么我将无法删除顶部并获得约束违规的例外。

I want to delete both pizza & topping separately irrespective of relationship ie; 我想分别删除披萨和浇头,而不考虑关系,即; I want to update the join table as well as delete from both sides. 我想更新联接表以及从双方删除。 How can I do that? 我怎样才能做到这一点?

Actually what I want is to delete pizza and topping separately ie; 实际上,我想要的是分别删除披萨和浇头,即; Like if I delete pizza it get deleted and updates the join table similarly when I delete the topping it get deleted updating the relationship in join table. 就像删除披萨一样,删除披萨也将更新连接表,而删除顶部时,删除披萨将更新连接表中的关系。 I don't mean that if I delete topping, pizza get deleted or vice-vesra. 我并不是说,如果我删除浇头,则比萨饼将被删除或变成副牌。

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

Add this on both sides. 在两面都添加。 I hope this will solve your problem. 我希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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