<i id='HXLF3'><tr id='HXLF3'><dt id='HXLF3'><q id='HXLF3'><span id='HXLF3'><b id='HXLF3'><form id='HXLF3'><ins id='HXLF3'></ins><ul id='HXLF3'></ul><sub id='HXLF3'></sub></form><legend id='HXLF3'></legend><bdo id='HXLF3'><pre id='HXLF3'><center id='HXLF3'></center></pre></bdo></b><th id='HXLF3'></th></span></q></dt></tr></i><div id='HXLF3'><tfoot id='HXLF3'></tfoot><dl id='HXLF3'><fieldset id='HXLF3'></fieldset></dl></div>

    <bdo id='HXLF3'></bdo><ul id='HXLF3'></ul>
  • <legend id='HXLF3'><style id='HXLF3'><dir id='HXLF3'><q id='HXLF3'></q></dir></style></legend>

      <tfoot id='HXLF3'></tfoot>

      <small id='HXLF3'></small><noframes id='HXLF3'>

      1. Spring Data Rest中嵌套资源的分页

        时间:2024-08-23

            • <tfoot id='vKban'></tfoot>
              <i id='vKban'><tr id='vKban'><dt id='vKban'><q id='vKban'><span id='vKban'><b id='vKban'><form id='vKban'><ins id='vKban'></ins><ul id='vKban'></ul><sub id='vKban'></sub></form><legend id='vKban'></legend><bdo id='vKban'><pre id='vKban'><center id='vKban'></center></pre></bdo></b><th id='vKban'></th></span></q></dt></tr></i><div id='vKban'><tfoot id='vKban'></tfoot><dl id='vKban'><fieldset id='vKban'></fieldset></dl></div>
                <bdo id='vKban'></bdo><ul id='vKban'></ul>
              • <small id='vKban'></small><noframes id='vKban'>

                  <tbody id='vKban'></tbody>

                1. <legend id='vKban'><style id='vKban'><dir id='vKban'><q id='vKban'></q></dir></style></legend>
                2. 本文介绍了Spring Data Rest中嵌套资源的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当访问下面的 URL 时,我得到分页响应

                  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 Overhead exceeded 错误,因为返回结果的行数很大.

                  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 Data搜索查询: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 Rest中嵌套资源的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Spring Data Mongo - 如何映射继承的 POJO 实体? 下一篇:Spring mongo 存储库切片

                  相关文章

                    <small id='BnPYp'></small><noframes id='BnPYp'>

                      <legend id='BnPYp'><style id='BnPYp'><dir id='BnPYp'><q id='BnPYp'></q></dir></style></legend>

                        <bdo id='BnPYp'></bdo><ul id='BnPYp'></ul>
                    1. <i id='BnPYp'><tr id='BnPYp'><dt id='BnPYp'><q id='BnPYp'><span id='BnPYp'><b id='BnPYp'><form id='BnPYp'><ins id='BnPYp'></ins><ul id='BnPYp'></ul><sub id='BnPYp'></sub></form><legend id='BnPYp'></legend><bdo id='BnPYp'><pre id='BnPYp'><center id='BnPYp'></center></pre></bdo></b><th id='BnPYp'></th></span></q></dt></tr></i><div id='BnPYp'><tfoot id='BnPYp'></tfoot><dl id='BnPYp'><fieldset id='BnPYp'></fieldset></dl></div>

                    2. <tfoot id='BnPYp'></tfoot>