英文:
Hibernate and Jackson manyToMany relation with extra fields
问题
最近我一直在尝试通过多对多关系链接一个Module表和一个Activity表。这种关系引入了一个额外的字段,如下所示。
-------------------------------------------------------------
| Module | ModuleHasActivities | Activities |
-------------------------------------------------------------
| id | Module_idModule | id |
| Name | Activity_idActivity | Name |
| Observations | Day | Price |
-------------------------------------------------------------
我使用JsonManagedReference和JsonBackReference来避免无限循环。不幸的是,结果并不是预期的,因为我无法从activities获取到modules列表,反之亦然。返回的唯一信息是实体字段。
@Entity
@Table(name = "Module")
public class Module implements Serializable {
@Id
@GeneratedValue
@Column(name = "idModule")
private int id;
// 其他字段加上getter和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;
// 其他字段
@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;
}
查询模块的响应:
[
{
"id": 1,
"code": "TT1",
"title": "Test 1",
"description": "Nothing",
"observations": "Nothing"
}
]
我期望的结果(类似于这样):
[
{
"id": 1,
"code": "TT1",
"title": "Test 1",
"description": "Nothing",
"observations": "Nothing",
"activities": [
"day1": {
// activities的字段
},
"day2": {
// activities的字段
}
]
}
]
当我调试Hibernate的SQL查询时,我们可以看到连接从未执行。这正是我所关心的!连接到模块表的连接丢失了。
对此有一点帮助将不胜感激,
谢谢
已访问链接:
Extra fields for many to many relations
英文:
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
答案1
得分: 0
你需要理解 JsonManagedReference
和 JsonBackReference
之间的区别。详见:https://stackoverflow.com/a/37394318/607637。
@Entity
public class Module {
@JsonBackReference
private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}
@Entity
public class ModuleHasActivities implements Serializable {
@JsonManagedReference
private Module module;
}
在这里,如果你序列化 Module 对象,你的 JSON 中将不会包含 moduleHasActivities
字段,但是如果你序列化 ModuleHasActivities 对象,你将会得到 module
字段。
所以,解决方案是将 @JsonBackReference
和 @JsonManagedReference
进行调换,如下所示:
@Entity
public class Module {
@JsonManagedReference
private Set<ModuleHasActivities> moduleHasActivities = new HashSet<>();
}
@Entity
public class ModuleHasActivities implements Serializable {
@JsonBackReference
private Module module;
}
英文:
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;
}
答案2
得分: 0
终于,我解决了我的问题。我不再使用复合键,而是将我的数据库模型更改为使用主 id 键和两个指向我关注的表的引用。映射更简单,我可以获取到所需信息。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论