0

I have a many-to-many table between Employees - SchoolDepartment EmployeeSchoolDepartment

But I could not manage to solve recursion between them. I could ignore the Set but i dont want to do it i want to show the list without recursion.

Here is the entity files :

Employee :

@Id
@Column(name="`UserId`",nullable=false)
private int userId;

@Column(name="`FirstName`",nullable=false)
private String firstName;

@Column(name="`LastName`",nullable=false)
private String lastName;


@Column(name="`NationalityId`",unique=true,nullable=false)
private String nationalityId;

@Column(name="`BirthOfDate`",nullable=false)
private Date birthOfDate;

@OneToOne(fetch = FetchType.LAZY)   
@JoinColumn(name="`UserId`")
private User user;

@OneToMany(mappedBy="employee")
@JsonIgnore()
Set<EmployeeSchoolDepartment> employeeSchoolDepartments;

SchoolDepartment

  @Id()
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private int id;
   
   
   @Column(name = "department",nullable = false)
   private String department;
   
   @ManyToOne()
   @JoinColumn(name =  "school_id")
   private School school;
   
   
   @OneToMany(mappedBy="schoolDepartment")
   Set<EmployeeSchoolDepartment> employeeSchoolDepartments;

EmployeeSchoolDepartment

@EmbeddedId
EmployeeSchoolDepartmentId id;

@Column(name = "start_date")
private Date startDate;

@Column(name = "graduate_date")
private Date graduateDate;



@ManyToOne()
@MapsId("employee_id")
@JoinColumn(name="employee_id")
private Employee employee;

@ManyToOne()
@MapsId("school_deparment_id")
@JoinColumn(name="school_department_id")
private SchoolDepartment schoolDepartment;
Halit Kalayci
  • 99
  • 2
  • 9
  • 1
    Does this answer your question? [Infinite Recursion with Jackson JSON and Hibernate JPA issue](https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – Nikolai Shevchenko Jun 01 '21 at 08:24

1 Answers1

0

You need to change EmployeeSchoolDepartment.

@MapsId is not correct in this case because it only works if the value of the association can be used as an id. In this case you need a composite id because is the combination of the two associations that makes the id.

You can pick one of these two approaches:

  1. Composite identifiers with @IdClass:
@Entity
@IdClass(EmployeeSchoolDepartmentId.class)
class EmployeeSchoolDepartment {

    @Id
    @ManyToOne
    @JoinColumn(name="employee_id")
    private Employee employee;

    @Id
    @ManyToOne
    @JoinColumn(name="school_department_id")
    private SchoolDepartment schoolDepartment;

    @Column(name = "start_date")
    private Date startDate;

    @Column(name = "graduate_date")
    private Date graduateDate;

}

class EmployeeSchoolDepartmentId implements Serializable {

    private Employee employee;

    private SchoolDepartment schoolDepartment;

    public EmployeeSchoolDepartmentId(Employee employee, SchoolDepartment schoolDepartment) {
        this.employee = employee;
        this.schoolDepartment = schoolDepartment;
    }

    private EmployeeSchoolDepartmentId() {
    }

    //Getters and setters are omitted for brevity
}
  1. Composite identifiers with @EmbeddedId:
@Entity
class EmployeeSchoolDepartment {

    @EmbeddedId
    EmployeeSchoolDepartmentId id;

    @Column(name = "start_date")
    private Date startDate;

    @Column(name = "graduate_date")
    private Date graduateDate;
}

@Embeddable
class EmployeeSchoolDepartmentId implements Serializable {

    @ManyToOne
    @JoinColumn(name="employee_id")
    private Employee employee;

    @ManyToOne
    @JoinColumn(name="school_department_id")
    private SchoolDepartment schoolDepartment;

    public EmployeeSchoolDepartmentId(Employee employee, SchoolDepartment schoolDepartment) {
        this.employee = employee;
        this.schoolDepartment = schoolDepartment;
    }

    private EmployeeSchoolDepartmentId() {
    }

    //Getters, setters, hashcode and equals are omitted for brevity
}
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Hello Davide, Thank you. I'll try and reply the result. What if i wanna join this many-to-many relationship from JPQL? Is it same as the one-to-many? – Halit Kalayci Jun 01 '21 at 08:59
  • It should be the same. – Davide D'Alto Jun 01 '21 at 09:06
  • By the way, my mapping won't fix your problem if it's related to the JSON but it is still the correct way to map those associations. You should add the exception you are having to the question. – Davide D'Alto Jun 01 '21 at 09:13