java JPA 多对多级联问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/951632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:32:39  来源:igfitidea点击:

JPA Many to Many cascade problem

javahibernatejpa

提问by ScArcher2

If I create a Customer and Controller, then associate my Controller with a customer it saves fine.

如果我创建一个客户和控制器,然后将我的控制器与它保存的客户相关联。

If I then remove my controller it doesn't remove the relationship between them. This causes an EntityNotFoundException when I load the Customer.

如果我然后删除我的控制器,它不会删除它们之间的关系。当我加载客户时,这会导致 EntityNotFoundException。

javax.persistence.EntityNotFoundException: Unable to find Controller with id 22

I'd like to know how to map this so that when a Controller is deleted the relationship is also deleted.

我想知道如何映射它,以便在删除控制器时也删除关系。

Database Tables

数据库表

  • customer
  • controller
  • customer_controllers - mapping table.
  • 顾客
  • 控制器
  • customer_controllers - 映射表。

The Controller's id is not getting removed from the customer_controllers mapping table.

控制器的 id 没有从 customer_controllers 映射表中删除。

@Entity
public class Customer implements Serializable{

    private Integer id;
    private Set<Controller> controllers;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @ManyToMany(cascade={CascadeType.ALL})
    public Set<Controller> getControllers()
    {
            return controllers;
    }
    public void setControllers(Set<Controller> controllers)
    {
            this.controllers = controllers;
    }
}

@Entity
public class Controller implements Serializable{

    private Integer id;
    private String name;
    private String abbreviation;

    @Id
    @GeneratedValue
    public Integer getId()
    {
            return id;
    }
    public void setId(Integer id)
    {
             this.id = id;
    }
    public String getName()
    {
             return name;
    }
    public void setName(String name)
    {
             this.name = name;
    }
    public String getAbbreviation()
    {
             return abbreviation;
    }
    public void setAbbreviation(String abbreviation)
    {
         this.abbreviation = abbreviation;
    }
}

回答by Pere Villega

If you have a ManyToMany then you should map Controller to Customer with a

如果你有一个多对多,那么你应该用一个映射控制器到客户

 @ManyToMany(mappedBy="controllers")

or the other way around, depending on which side is the owning side.

或者反过来,取决于哪一方是拥有方。

As you have it now the relation is not fully defined and it will fail on events like "Cascade".

正如您现在拥有的那样,该关系尚未完全定义,并且会在“级联”等事件上失败。

回答by OMax

Have you checked the javadoc for @ManyToMany? It includes the above example mappings.

你检查过@ManyToMany的 javadoc吗?它包括上面的示例映射。

回答by mR_fr0g

you need to make the relationship bidirectional, so that the controller object is aware of its relationship to the customer. Yhis means that when the controller is deleted the record in the join table is also deleted.

您需要使关系双向,以便控制器对象知道它与客户的关系。Yhis 意味着当控制器被删除时,连接表中的记录也被删除。

This isn't the exact mapping but it gives you the idea.

这不是确切的映射,但它为您提供了想法。

@Entity
public class Controller implements Serializable{

    private Integer id;
    private String name;
    private String abbreviation;


    private Set<Customer> customers;


    @Id
    @GeneratedValue
    public Integer getId()
    {
            return id;
    }
    public void setId(Integer id)
    {
             this.id = id;
    }
    public String getName()
    {
             return name;
    }
    public void setName(String name)
    {
             this.name = name;
    }
    public String getAbbreviation()
    {
             return abbreviation;
    }
    public void setAbbreviation(String abbreviation)
    {
         this.abbreviation = abbreviation;
    }
    @ManyToMany(cascade={CascadeType.ALL})
    public Set<Customer> getCustomers()
    {
        return customers;
    }
    public void setCustomers(Set<Customers> customers)
    {
        this.customers= customers;
    }

}