春季JPA投影,包括链接 [英] Spring JPA Projection including links

查看:79
本文介绍了春季JPA投影,包括链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个简单的Event模型,该模型具有一组Booking对象:

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

事件:

@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接口,并且我已经创建了一个投影,以便在检索事件时可以包括预订的详细信息.

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();
}

这是因为可以正确返回预订,但是预订对象没有它的_links对象,就像我自己检索它们时那样.如何获取带有关联链接的预订对象,以便可以对已获取的预订对象执行操作?

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?

即代替:

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

推荐答案

最新版本的spring-data快照已解决此问题,默认情况下包含链接.通过更新我的POM使其包含以下内容,链接将出现在嵌入式集合中

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>

这篇关于春季JPA投影,包括链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆