编程语言
首页 > 编程语言> > java-Spring JPA Projection包括链接

java-Spring JPA Projection包括链接

作者:互联网

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

事件:

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

解决方法:

最新的spring-data快照版本已解决此问题,并且默认情况下包含链接.通过更新我的POM以包括以下内容,链接将显示在嵌入式收藏夹中

    <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>

标签:spring-data-rest,spring-data,jpa,spring,java
来源: https://codeday.me/bug/20191120/2042140.html