最近我尝试创建多对一和一对多的 CRUD。我得到了以下奇怪的结果,但我无法处理它。
这是我得到的来自/teams 的 JSON
[
{
"id": 3,
"name": "team1",
"footballers": [
{
"id": 12,
"name": "karol",
"age": 0,
"team": {
"id": 3,
"name": "team1",
"footballers": [
12,
{
"id": 13,
"name": "Pauluszka",
"age": 0,
"team": 3
}
]
}
},
13
]
}
]
我想要实现的是
[
{
"id": 3,
"name": "team1",
"footballers": [
{
"id": 12,
"name": "karol",
"age": 0
},
{
"id": 13,
"name": "Pauluszka",
"age": 1
}
]
}
]
这是我的 Pojo
足球运动员.java
...somecodehere
@ManyToOne
@JoinColumn(name="team_id")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class , property = "id")
private Team team;
...somecodehere
团队.java
...somecodehere
@OneToMany(mappedBy = "team")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
private List<Footballer> footballers;
...somecodehere
如果您能为我提供一些提示,我将不胜感激,我应该改变什么才能实现我想要的目标。
最佳答案
使用@JsonManagedReference
和@JsonBackReference
这里有示例http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
您可以使用@JsonView
注释来指定要在选定 View 中序列化的字段。这将允许您根据需要省略 player
的 team
属性。
关于java - Spring Data JPA 到深入而奇怪的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48082897/