给定一个简单的事件模型,其中包含一组预订对象:
事件:
每个都有一个JpaRepository接口,我创建了一个投影,以便在检索事件时包含预订的详细信息。
这个代码可以正确返回预订信息,但是预订对象没有_links属性,如果我单独检索预订对象,它们应该有这个属性。如何获取带有关联链接的预订对象,以便对已检索到的预订对象执行操作?
例如,不是:
事件:
@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
}
预订:
@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"
}
}
}