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.
A little help would be appreciated,
Thank you
Already visited links : Extra fields for many to many relations