Spring JPA投影包括链接

3
给定一个简单的事件模型,其中包含一组预订对象:
事件:
@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"
      }
   }
}
1个回答

3

这个问题已经在最新的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 Boot 1.2.8和Gosling-SR4 releasetrain的Spring Data REST,但是在投影中无法获取链接对象的_links属性。您是否仍然默认看到此行为? - Marc Zampetti
我无法评论那个版本,我目前正在使用Spring Boot 1.3.2.RELEASE,其中包含Spring Data组件2.4.2.RELEASE,并且_links属性出现了。您能升级到更新的Spring Boot吗? - Drew
我无法升级到 Spring Boot 1.3.x,但我正在使用 Spring Data Rest 2.4.4.RELEASE。Spring Boot 版本不应该有影响,但它可能与其他依赖关系有关。 - Marc Zampetti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接