我有一个项目处理对象的一些 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
}
这可能吗?有人可以给我一些关于如何做到这一点的见解吗?我只是找到有关如何做相反的教程(我已经有了的解决方案)。