Spring Data Data中的分页用于嵌套资源 [英] Pagination in Spring Data Rest for nested resources

查看:87
本文介绍了Spring Data Data中的分页用于嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当访问以下网址时,我会得到分页作为回复

When the below URL is visited, I get paginations in response

/api/userPosts/

{
  "_links" : {
    "self" : {
      "href" : "/api/userPosts{?page,size,sort}",
      "templated" : true
    },
    "next" : {
      "href" : api/userPosts?page=1&size=20{&sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "userPosts" : [ {
     ...

然而,当访问以下URL时,Spring Data REST没有开箱即用的分页 -

However when visiting following URL, there is no pagination out of box by Spring Data REST -

/api/users/4/userPosts

{
  "_embedded" : {
    "userPosts" : [ {

UserRepository和UserPostRepository都是带有分页的JPARepository。因此,第二个URL会导致GC开销超出错误,因为返回结果的行数很大。

Both UserRepository and UserPostRepository are JPARepository with pagination. As a result, the second URL is throwing GC overhead exceeded error since the row count for results returned is huge.

@RepositoryRestResource(excerptProjection = UserProjection.class)
public interface UserRepository extends BaseRepository<User, Integer>, UserRepositoryCustom {

}

public interface UserPostRepository extends BaseRepository<UserPost, Long> {

}


@NoRepositoryBean
public interface BaseRepository<T, N extends Serializable> extends JpaRepository<T, N>, QueryDslPredicateExecutor<T> {

}

是否也可以使用第二个URL进行分页?

Any way to have pagination with second URL as well ?

推荐答案

简短的回答:不。绝对不是。

Short painful answer: No. Absolutely not.

长得更痛苦的回答:是的。通过重写Spring Data,JPA和Hibernate的大部分内容。
问题的核心是,当您请求嵌套实体(集合与否)时,嵌套实体不是来自存储库的查询。但是是从实体返回。 Spring Data / JPA中没有用于分页的机制

Long even more painful answer: Yes. By rewriting large sections of Spring Data, JPA and Hibernate. The core of the problem is that when you are requesting the the nested entity (collection or not) that nested entity is NOT queries from repository. But is is returned from the entity. There are no mechanics in Spring Data / JPA for paging

Spring REST中的/ api / users / 4 / userPosts请求基本上是这样的:

What /api/users/4/userPosts request in Spring REST does is basicly this:

User user = userRepository.findOne(4);
return user.userPosts;

因此,检索user.userPosts是对嵌套实体的Eager或Lazy引用,并且无法页面那。

So retrieving user.userPosts is Eager or Lazy reference to an nested entity and there is not way to page that.

实现此目的的最简单且唯一的解决方案是:
1.创建Spring数据搜索查询:UserPostRepository.findByUserId(Long id,Pagination pa)
2.创建用于映射的自定义Spring MVC控制器

Easiest and only solution to achieve this is : 1. create Spring Data search query: UserPostRepository.findByUserId(Long id, Pagination pa) 2. Create custom Spring MVC controller for mapping

   @Get("/api/users/{id}/userPosts")
   public Page<UserPost> getUserPostsByUserId(Long id, Pagination pagi) {
     return userPostRepository.findByUserId(id, pagi);




  1. 重要!你不能!将user.userPosts注释为嵌套在User实体中或请求映射将发生冲突。

  2. 如果您想要在用户实体JSON中为此路径导航超链接,那么您需要
    自定义处理来创建用户实体JSON。它的文档很少,没有一个例子涵盖了你需要探索的确切用例。

这篇关于Spring Data Data中的分页用于嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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