Spring JPA Projection including links

2020-07-22 03:46发布

问题:

Given a simple model of Event that has a Set of Booking objects:

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
}

Each have a JpaRepository interface, and I've created a projection so that I can include the details of the booking 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 is working in that the bookings are correctly returned, however the booking object doesn't have its _links object as it would do if I'd retrieved them on their own. How can I retrieve the booking objects with their associated links so that I can perform operations on the booking objects that have been retrieved?

i.e. instead of:

{  
   "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 get:

    {  
   "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"
      }
   }
}

回答1:

This has been resolved in the latest snapshot release of spring-data, and links are included by default. By updating my POM to include those show below, links appeared on embedded 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>