【发布时间】:2016-02-02 04:40:40
【问题描述】:
我有一个项目处理一些对象的 ORM 映射(有一些 @OneToMany 关系等)。
我使用 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
}
这可能吗?有人能给我一些关于如何做到这一点的见解吗?我只是在寻找关于如何做相反的教程(我已经拥有的解决方案)。
【问题讨论】:
-
你可以试试发现这个有用 - wiki.fasterxml.com/JacksonFeatureObjectIdentity
标签: java json hibernate orm spring-boot