我有如下所示的json,它来自第三方服务,我通过开放的伪装客户端获取它。我正在尝试使用Spring JPA2将这些详细信息保存到我的MySQL数据库中。
[
{
"request_id": 111,
"name": "ABC",
"groups": [
{
"id": 21,
"group": "grp_A"
},
{
"id": 22,
"group": "grp_B"
}
]
},
{
"request_id": 222,
"name": "ABC",
"groups": [
{
"id": 23,
"group": "grp_C"
}
]
},
{
"request_id": 333,
"name": "ABC",
"groups": [
{
"id": 24,
"group": "grp_A"
}
]
},
{
"request_id": 444,
"name": "ABC",
"groups": [
{
"id": 25,
"group": "grp_C"
}
]
},
{
"request_id": 555,
"name": "ABC",
"groups": []
}
]
我已经为上述json编写了映射,如下所示
Requests.java
@Entity
@Table(name = "requests")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Requests {
@Id
@Column(name = "request_id")
private String id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Groups> groups = Sets.newHashSet();
// getters and setters
}
Groups.java
@Entity
@Table(name = "groups")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Groups{
@Id
@Column(name = "id")
private String id;
@Column(name = "group")
private String group;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "request_id")
private Requests requests;
// getters and setters
}
现在在服务类中,我已经完成了如下所示的操作
List<Requests> allRequests = requestFeignClient.getRequests();
requestRepository.saveAll(allRequests);
除 request_id 之外的所有详细信息都保存到数据库中,如下所示。我的预期分组表也显示在下方
任何人都可以告诉我为什么未保存 request_id 。
答案 0 :(得分:1)
尝试替换此:
@OneToMany(mappedBy = "id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
与此:
@OneToMany(mappedBy = "requests", cascade = CascadeType.ALL, fetch = FetchType.LAZY)