0

Recently I've been trying to link a Module table and an Activity table by a Many to Many relationship. This relation welcomes an additional field as below.

-------------------------------------------------------------
| Module        |   ModuleHasActivities |     Activities    |
-------------------------------------------------------------
| id            |  Module_idModule      |   id              |   
| Name          |  Activity_idActivity  |   Name            |
| Observations  |  Day                  |   Price           |
-------------------------------------------------------------

I use JsonManagedReference and JsonBackReference to avoid infinite loops. Unfortunately the result is not the expected one since I don't get the list of modules from the activities and vice versa. The only information returned are the entity fields.

@Entity
@Table(name = "Module")
public class Module implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "idModule")
    private int id;
    
    // Other fields plus getter and setter

    @OneToMany(mappedBy = "module", cascade = CascadeType.ALL)
    @JsonBackReference
    private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}
@Entity
@Table(name = "Activite")
public class Activite implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "idActivite")
    private int id;

    // Other fields

    @OneToMany(mappedBy = "activite", cascade = CascadeType.ALL)
    @JsonBackReference
    private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}
@Entity
@Table(name = "Module_has_Activite")
public class ModuleHasActivities implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "Module_idModule")
    @JsonManagedReference
    private Module module;

    @Id
    @ManyToOne
    @JoinColumn(name = "Activite_idActivite")
    @JsonManagedReference
    private Activite activite;

    @Column(name = "IdJour")
    private int jour;

}

Query modules response:

[
    {
        "id": 1,
        "code": "TT1",
        "title": "Test 1",
        "description": "Nothing",
        "observations": "Nothing",
    }
]

What I expect (something like this) :

[
    {
        "id": 1,
        "code": "TT1",
        "title": "Test 1",
        "description": "Nothing",
        "observations": "Nothing",
        "activities": [
          "day1": 
          {
           //fields of activities
          },
          "day2": 
          {
           //fields of activities
          },
    ]
    }
]

When I debug hibernate's SQL queries, we see that the join is never done. And that's what interests me! Joints to the module table are missing. Joints to the module table are missing

A little help would be appreciated,

Thank you

Already visited links : Extra fields for many to many relations

nicooooo
  • 33
  • 5

2 Answers2

0

You need to understand the differences between JsonManagedReference and JsonBackReference. See this: https://stackoverflow.com/a/37394318/607637 for more info.

@Entity
public class Module  {
    @JsonBackReference
    private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}

@Entity
public class ModuleHasActivities implements Serializable {
    @JsonManagedReference
    private Module module;
}

Here, if you serialize Module object, you will not get moduleHasActivities field in json but if you serialize ModuleHasActivities object you will get module field.

So, the solution is to flip the @JsonBackReference and @JsonManagedReference as below:

@Entity
public class Module  {
    @JsonManagedReference
    private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}

@Entity
public class ModuleHasActivities implements Serializable {
    @JsonBackReference
    private Module module;
}
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • Ok first thank you for your answer. I tried your solution and I have the inner join query in the debug trace. But that's an infinite recursion error! In my ModuleHasActivities, Module field is in JsonBackReference as you suggested and I let Activity field in JsonManagedReference in order to get all activities. It's probably wrong – nicooooo Oct 23 '20 at 07:56
  • any help @gtiwari333 ? – nicooooo Oct 26 '20 at 08:11
0

Finally, I solved my problem. Instead of using a composite key, I changed my database model to use a primary id key and 2 references to my concerned tables. Mapping is easier and I get my information.

nicooooo
  • 33
  • 5