给定一个简单的 Event 模型,它具有一组 Booking 对象:
Event:
@Entity
public class Event {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long eventId;
private Date start;
private Date end;
private String title;
@OneToMany(mappedBy="event")
private Set<Booking> Bookings;
protected Event() {
// for JPA
}
// Getters and setters omitted for brevity
}
Booking:
@Entity
public class Booking {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long bookingId;
private String title;
private String contact;
@ManyToOne
@JoinColumn(name="event_id", nullable=false)
private Event event;
public Booking() {
// for JPA
}
// Getters and setters omitted for brevity
}
每个都有一个 JpaRepository 接口,并且我创建了一个投影,以便在检索事件时可以包含预订的详细信息。
@Projection(name="with-booking", types=Event.class)
public interface EventWithBookingProjection {
public Date getStart();
public Date getEnd();
public String getTitle();
public List<Booking> getBookings();
}
这是有效的,因为预订被正确返回,但是预订对象没有它的 _links 对象,就像我自己检索它们一样。如何检索预订对象及其关联链接,以便对已检索到的预订对象执行操作?
即代替:
{
"id":1,
"title":"Test Title",
"bookings":[
{
"title":"Test 1",
"contact":"Contact 1"
},
{
"title":"Test 2 ",
"contact":"Contact 2"
}
],
"end":"2015-06-06T11:30:00.000+0000",
"start":"2015-06-06T09:00:00.000+0000",
"_links":{
"self":{
"href":"http://localhost:8080/rest/events/1{?projection}",
"templated":true
},
"bookings":{
"href":"http://localhost:8080/rest/events/1/bookings"
}
}
}
我想得到:
{
"id":1,
"title":"Test Title",
"bookings":[
{
"title":"Test 1",
"contact":"Contact 1",
"_links":{
"self":{
"href":"http://localhost:8080/rest/bookings/23{?projection}",
"templated":true
},
"event":{
"href":"http://localhost:8080/rest/bookings/23/event"
}
}
},
{
"title":"Test 2 ",
"contact":"Contact 2",
"_links":{
"self":{
"href":"http://localhost:8080/rest/bookings/24{?projection}",
"templated":true
},
"event":{
"href":"http://localhost:8080/rest/bookings/24/event"
}
}
}
],
"end":"2015-06-06T11:30:00.000+0000",
"start":"2015-06-06T09:00:00.000+0000",
"_links":{
"self":{
"href":"http://localhost:8080/rest/events/1{?projection}",
"templated":true
},
"bookings":{
"href":"http://localhost:8080/rest/events/1/bookings"
}
}
}