嵌套的对象我有一个项目,该项目使用对象的一些ORM映射交易(也有一些@OneToMany
关系等)。春天开机JPA - JSON而不一对多关系
我使用REST接口来处理这些对象和Spring JPA以在API中管理它们。
这是我的POJO中的一个例子:
@Entity
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String dateOfDeparture;
private double distance;
private double price;
private int seats;
@ManyToOne(fetch = FetchType.EAGER)
private Destination fromDestination;
@ManyToOne(fetch = FetchType.EAGER)
private Destination toDestination;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "flight")
private List<Reservation> reservations;
}
发出请求的时候,我必须指定在JSON的一切:
{
"id": 0,
"reservations": [
{}
],
"name": "string",
"dateOfDeparture": "string",
"distance": 0,
"price": 0,
"seats": 0,
"from": {
"id": 0,
"name": "string"
},
"to": {
"id": 0,
"name": "string"
}
}
我喜欢什么,实际上是指定引用对象的id而不是它们的整个身体,如下所示:
{
"id": 0,
"reservations": [
{}
],
"name": "string",
"dateOfDeparture": "string",
"distance": 0,
"price": 0,
"seats": 0,
"from": 1,
"to": 2
}
这是甚至是possi竹叶提取?有人能给我一些关于如何做到这一点的见解吗?我只找到如何做相反的教程(我已经有了解决方案)。
你可以尝试找到这个有用的 - http://wiki.fasterxml.com/JacksonFeatureObjectIdentity – VadymVL