简体   繁体   English

拯救双方的多对多关系

[英]Saving Many-To-Many relations from both sides

I found many interesting answers in this subject but not any of them is correct for me. 我在这个问题上发现了许多有趣的答案,但其中没有一个对我来说是正确的。

Given part of Job.java 给定Job.java的一部分

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "company_employee",
        joinColumns = @JoinColumn(name = "job_id"),
        inverseJoinColumns = @JoinColumn(name = "employee_id"))
private Set<Employee> employees = new HashSet<>();

and part of Employee.java Employee.java的一部分

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "company_employee",
        joinColumns = @JoinColumn(name = "employee_id"),
        inverseJoinColumns = @JoinColumn(name = "job_id"))
private Set<Job> jobs = new HashSet<>();

How it is not working for both sides? 双方都不怎么运作?

Creating objects: 创建对象:

Job job = job.builder().build();
jobRepository.save(job);

Employee employee = Employee.builder().jobs(Sets.newSet(job));
employeeRepository.save(employee);

As I do it in this way - only one side has saved ManyToMany, when tested Employee has a Job but Job does not have any Employee. 按照我的方式,当测试的Employee有工作,但Job没有任何Employee时,只有一侧保存了ManyToMany。 Shouldn't Hibernate take care of this?` 休眠不应该照顾这个吗?

job.getEmployees = null;
employee.getJobs = { ...job... };

I need this to be done from both sides, so when Employee is saved first, you can add him from Job side. 我需要从两侧进行此操作,因此当首先保存Employee时,可以从Job端添加他。 MappedBy, Cascade, I think I've tried most of them. 通过,Cascade,我想我已经尝试了其中的大多数。 Also - I tried with extra "add" method but it's not working in this case, StackOverflowError... 另外-我尝试了额外的“添加”方法,但在这种情况下无法正常工作,StackOverflowError ...

You create two different tables for the same relation. 您为同一关系创建两个不同的表。

If you want to create bi-directional ManyToMany relation, just create one table, and with second collection use "mappedBy" attribute of @ManyToMany annotation 如果要创建双向ManyToMany关系,只需创建一个表,然后使用第二个集合,使用@ManyToMany批注的“ mappedBy”属性

     @ManyToMany(mappedBy="employees", cascade = CascadeType.ALL)
     private Set<Job> jobs = new HashSet<>();

For more information check HERE 欲了解更多信息,请点击这里

When you add annotation @JoinTable, you are telling hibernate to create a junction table between the 2 entities. 当添加注解@JoinTable时,您要告诉hibernate在2个实体之间创建一个联结表。 Adding on both sides will mean double definition. 双方加总意味着双重定义。 Why is this bad? 为什么这样不好? If you have 2 different definitions, which one should hibernate choose? 如果您有2种不同的定义,那么冬眠应该选择哪一种?

You don't have to save first the jobs and after the employees, if they will have cascade option, hibernate will do that for you. 您不必先保存工作,然后再保存员工,如果他们可以选择级联,则休眠将为您完成。

How to solve null problem? 如何解决空问题? If you have many to many relationship, it means that you have a bidirectional relationship and they should have references to each other. 如果您具有多对多关系,则意味着您具有双向关系,并且它们之间应该相互引用。

   employee.getJobs().add(job);
   job.getEmployees().add(employee);

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

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