简体   繁体   English

jpa 多对多取回数据

[英]jpa many to many getting back data

I have two entity employee:我有两个实体员工:

public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int emp_id;

    private String name;

    private String email;

    private String phone;

    @ManyToMany(
            fetch = FetchType.LAZY, cascade = CascadeType.ALL

    )
    @JoinTable(name = "EMP_PRO_DB", joinColumns = {
            @JoinColumn(name = "empId", referencedColumnName = "emp_id")
    },
    inverseJoinColumns = {
            @JoinColumn(name = "proId", referencedColumnName = "pro_id")
    })
    private Set<Project> projects = new HashSet<>();

}

Project:项目:

public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int pro_id;

    private String projectName;

    private String projectType;
    @JsonIdentityInfo(
            generator = ObjectIdGenerators.PropertyGenerator.class,
            property = "emp_id"
    )
    @ManyToMany(
            fetch = FetchType.LAZY, cascade = CascadeType.ALL

    )
    @JoinTable(name = "EMP_PRO_DB", joinColumns = {
            @JoinColumn(name = "proId", referencedColumnName = "pro_id")
    },
            inverseJoinColumns = {
                    @JoinColumn(name = "empId", referencedColumnName = "emp_id")
            })
    private Set<Employee> employees = new HashSet<>();
}

When i fetch all data from employee, the json returns me:当我从员工那里获取所有数据时,json 返回给我: 在此处输入图像描述

which is correct, it can show me the projects that the employee has.这是正确的,它可以向我展示该员工拥有的项目。

BUT when i fetch all data from project, the json returns me:但是当我从项目中获取所有数据时,json 返回给我: 在此处输入图像描述

Employee 1 has two project, it shows one, employee 2 has one project but its only showing the ID, I will need the details of that employee (at leas the name)员工 1 有两个项目,它显示一个,员工 2 有一个项目但它只显示 ID,我需要该员工的详细信息(至少是姓名)

It seems you tried to create a bidirectional relationship between Employee and Project .您似乎试图在EmployeeProject之间创建双向关系。

But instead you created two conceptually independent relationships, one from Employee to Project and one in the reverse direction, that just happen to write into the same tables.但是您创建了两个概念上独立的关系,一个是从EmployeeProject ,另一个是相反的方向,它们恰好写入相同的表。 This of course is bound to cause troubles.这当然会引起麻烦。

The fix is to convert this into a proper bidirectional relationship.解决方法是将其转换为适当的双向关系。

Change the Project side of the relationship to将关系的Project端更改为

    @ManyToMany(
            fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedby="projects"
    )
    private Set<Employee> employees = new HashSet<>();

Note that with this mapping only the Employee side of the relationship will be used while persisting.请注意,使用此映射,在持久化时将仅使用关系的Employee端。 You'll have to keep the two sides in sync on the Java side yourself.您必须自己在 Java 端保持两侧同步。

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

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