Search code examples
hibernatejpamany-to-manyassociations

Many to Many relationship is returning many nested results


I have two tables [1]users and [2] employee_group
I am trying to implement ManyToMany relationship and this is how I implemented using annotation

@Entity
@Table(name = "employee_group")
public class EmployeeGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;
    
    @ManyToMany(mappedBy = "employeeGroups")
    private List<User> users;
    .........
    // other properties 
}

@Entity
@Table(name = "users")
public class User{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;

    @ManyToMany
    @JoinTable(name = "emp_group_map", 
    joinColumns = { @JoinColumn(name = "fk_user") }, 
    inverseJoinColumns = { @JoinColumn(name = "fk_emp_group") })
    private List<EmployeeGroup> employeeGroups;
    .........
    // other properties 
}

Expectation: I have one user (for example: userId 100) and two employee groups in which this user belongs, so I am expecting that when I try to get this user by userId saay 100 it will get user details with employee group list and this employeeGroups will be having two records.

Issue: This returns nested results like:

{
    "id": 11,       
    "employeeGroups": [
        {
            "id": 1,           
            "users": [
                {
                    "id": 11,                   
                    "employeeGroups": [
                        {
                            "id": 1,                            
                            "users": [
                                {
                                    "id": 11,                                   
                                    "employeeGroups": [
                                        {
                                            "id": 1,                                            
                                            "users": [
                                                {
                                                    "id": 11,                                                   
                                                    "employeeGroups": [
                                                        {                                                            
                                                            "id": 1,                                                            
                                                            "users": [
                                                                {
                                                                    "id": 11,

I stuck here and not getting that exactly what is wrong. I am using Spring-Boot 2.6.4.


Solution

  • Jackson will be going in to User, sees the employeeGroup so it goes in to that, which has a List<User> so it starts to go over those, again getting the user, then its employee group etc. This leads to the potential OOM.

    @JsonManagedReference @JsonBackReference may be of use to you.

    Also, consider using a DTO as I am assuming you are trying to return a user out of a controller (so jackson will map it).

    Using a DTO Data Transfer Object, you will de-couple the entity and the returned DTO from the controller and also more easily customise and handle what is returned and the structure of it.