Java Spring数据JPA到深度和怪异的映射
最近,我尝试创建具有多对一和一对多的CRUD。我有以下奇怪的结果,我无法处理 这是来自/teams的JSON,我得到了Java Spring数据JPA到深度和怪异的映射,java,json,spring,spring-data-jpa,crud,Java,Json,Spring,Spring Data Jpa,Crud,最近,我尝试创建具有多对一和一对多的CRUD。我有以下奇怪的结果,我无法处理 这是来自/teams的JSON,我得到了 [ { "id": 3, "name": "team1", "footballers": [ { "id": 12, "name": "karol", "age": 0, "team": { "id": 3,
[
{
"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
Footballer.java
...somecodehere
@ManyToOne
@JoinColumn(name="team_id")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class , property = "id")
private Team team;
...somecodehere
Team.java
...somecodehere
@OneToMany(mappedBy = "team")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
private List<Footballer> footballers;
...somecodehere
…这里有一些代码
@OneToMany(mappedBy=“团队”)
@JsonIdentityInfo(
生成器=ObjectedGenerators.PropertyGenerator.class,
property=“id”)
私人球员名单;
…这里有人吗
如果您能给我提供一些提示,我应该做些什么来达到我想要的效果,我将不胜感激。使用
@JsonManagedReference
和@JsonBackReference
这里有一个例子
您可以使用
@JsonView
注释指定要在所选视图中序列化的字段。这将允许您按需管理团队
玩家的属性。问题是?您想知道如何生成一些JSON吗?(在这种情况下,问题与JPA无关)或者您想知道如何持久化该Java模型?(在这种情况下没有JSON相关性)问题是在我的情况下我做错了什么,我实现了这个奇怪的JSON,而不是我想要的。好吧,那么与JPA API无关。