Spring Design JPA including links
Considering a simple model of an event with a "Reservation" object:
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
}
They each have a JpaRepository interface and I created a projection to include the booking information when retrieving the event.
@Projection(name="with-booking", types=Event.class)
public interface EventWithBookingProjection {
public Date getStart();
public Date getEnd();
public String getTitle();
public List<Booking> getBookings();
}
This works so that orders are returned correctly, but the reservation object does not have its own _links object as it would if I received them myself. How can I get the booking objects with their associated links so that I can operate on the booking objects that have been received?
i.e. instead:
{
"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"
}
}
}
I want to receive:
{
"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"
}
}
}
+3
source to share
1 answer
This was resolved in the latest version of spring-data snapshot and links are included by default. By updating my POM to include the ones shown below, the links appeared in the built-in collections.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.4.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.11.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>2.4.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.0.M1</version>
</dependency>
+3
source to share