Paginate sub-resources in Spring Data Rest 2.1

前端 未结 2 378
遥遥无期
遥遥无期 2020-12-16 06:02

I use Spring Data Rest 2.1.1 Release with the default configuration. Considering the following resource:

GET /communities/MyCommunity

{
    \"creationDate\"         


        
2条回答
  •  暖寄归人
    2020-12-16 06:29

    The answer given by Oliver is not entirely correct.

    On EAGER nested entity (collection) the is no repository usage. But on LAZY nested entity (collection) there IS repository usage. @One2Many by default is LAZY. So the nested entity is not populated nor is it retrieved from repo when the parent entity is retrieved. The nested entity is only populated when it it accessed within the session. In Spring Data REST listing of parent entity never accesses the nested entity. When the nested LAZY entity is accessed via the nested entity access URL ie. /communities/GroupeSEB/posts is the nested Object accessed and as that nested object is not an object instance BUT LazyProxy of the object that will cause retrieval from repo. Ok, the nested object could be retrieved from Repo layer cache insead of the repo, but I think you can prohibit cache on the nested entity/object.

    So if there was a way to pass/retrieve the paging info on LazyProxy implementation this could work.

    P.S. I know they from experience as I made bi-directional navigation between @One2Many and @Many2One and to prevent N+1 on listing parents I set @ManyToOne(fetch = FetchType.LAZY) and then override the related findbyId() in repo interface with @NamedEntityGraph that forced the nested to EAGER when retrieved with @NamedEntityGraph. This does not apply directly to this problem but does demonstrate that within spring data rest the retrieval of nesetd entities is controllable.

提交回复
热议问题