Spring Data Rest Pageable Child Collection [英] Spring Data Rest Pageable Child Collection
问题描述
我有一个名为 User 的 @Entity.它有一组变更集,如下所示:
I have an @Entity called User. It has a Set of Changesets as follows:
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
private Set<Changeset> changesets = new HashSet<Changeset>();
我有一个 UserRepository:
I have a UserRepository:
@Repository
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{ }
还有一个 ChangesetRepository:
And a ChangesetRepository:
@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
在 http://localhost:8080/changesets/
或 http://localhost:8080/users/
上调用 GET 会产生分页响应.
Calling GET on http://localhost:8080/changesets/
or http://localhost:8080/users/
yields a paged response.
如果我在 http://localhost:8080/users/1/changesets
上调用 GET ,那么我会在一个数组中获取所有结果并且不会发生分页.
If I call GET on http://localhost:8080/users/1/changesets
then I get all the results in a single array and no paging occurs.
有没有办法向 Spring Data Rest 指示我想在通过其父用户访问时以可分页的方式返回变更集集合?变更集集将快速增长,我宁愿不在单个页面中返回大量结果.
Is there a way to indicate to Spring Data Rest that I want to return the changesets collection in a pageable manner when accessing it through its parent User? The Set of changesets will grow quickly and I'd rather not return tons of results in a single page.
根据 Willie Wheeler 的建议,我将其添加到我的 ChangesetRepository 中以使其可搜索:
As suggested by Willie Wheeler I added this to my ChangesetRepository to make it searchable:
@RestResource(path = "byUser", rel = "byUser")
public Page<Changeset> findByUser(@Param("id") User user, Pageable p);
我保留了双向关系,但也能够通过在变更集集上使用 @RestResource(exported=false)
向用户隐藏指向变更集的链接.
I left the relationship bidirectional, but was also able to hide the link to changesets from a user by using @RestResource(exported=false)
on the Set of changesets.
旁注:似乎将关系设置为exported=false 隐藏了链接,但实际上并没有删除映射./users/1/changesets 没有公布,但它仍然有效.
Side Note: It appears that setting the relationship to exported=false hides the link, but doesn't actually remove the mapping. /users/1/changesets isn't advertised, but it's still valid.
推荐答案
我不认为 SDR 直接支持您描述的方法.
I don't think that SDR directly supports the approach you describe.
不过,您可以采用另一种方法.与其在 Person
和 Changeset
之间使用双向关系,不如让它从 Changeset
到 Person
是单向的.然后在您的 ChangesetRepository
中包含这样的方法:
There's another approach you might take though. Instead of using a bidirectional relationship between Person
and Changeset
, make it unidirectional from the Changeset
to the Person
. Then in your ChangesetRepository
include a method like this:
@RestResource(path = "find-by-person")
Page<Changeset> findByPerson(@Param("person") Person person, Pageable pageable);
(我只是凭记忆做的,所以您可能需要稍作调整.)
(I'm just doing that from memory, so you might require minor adjustments.)
从设计的角度来看,我认为这无论如何都更强,因为我认为一个人的变更集在有限数量的上下文中是相关的.查询变更集而不是将它们与人捆绑可能更合适.
From a design perspective I think this is stronger anyway, since I imagine that a person's changesets are relevant in a limited number of contexts. It might be more appropriate to query for changesets rather than bundling them with the person.
这篇关于Spring Data Rest Pageable Child Collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!