Spring Data Rest Pageable子集合 [英] Spring Data Rest Pageable Child Collection
问题描述
我有一个名为User的@Entity。它有一组变化集,如下所示:
@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = user)
private Set< Changeset> changesets = new HashSet< Changeset>();
我有一个UserRepository:
@Repository
@RestResource(path =users,rel =users)
public interface UserRepository扩展JpaRepository< User,Long> {}
@Repository
@RestResource(path =changesets,rel =changesets)
public interface ChangesetRepository extends JpaRepository< Changeset,Long> {
在 http:// localhost:8080 / changesets /
或 http:// localhost:8080 / users /
会产生一个分页回应。
如果我在 http:// localhost:8080 / users / 1 / changesets
上调用GET,那么我将所有结果都放在一个数组中,不会发生分页。
有没有一种方法可以向Spring Data Rest表明我想通过其父级用户访问它时以可分页方式返回changesets集合?这组变化集会快速增长,我宁愿不会在单个页面中返回大量结果。
编辑:
正如Willie Wheeler所建议的那样,我将此添加到我的ChangesetRepository中以使其可搜索:
@RestResource(path = byUser,rel =byUser)
public Page< Changeset> findByUser(@Param(id)用户用户,Pageable p);
我使用双向关系,但也可以隐藏用户的变更集链接 @RestResource(exported = false)
在变更集上。
方面注意:到exported = false隐藏链接,但不实际删除映射。 / users / 1 / changesets不会被公布,但它仍然有效。
我不认为SDR直接支持你描述的方法。
你还可以采取另一种方法。而不是使用 Person
和 Changeset
之间的双向关系,使它从 Changeset
到 Person
。然后在你的 ChangesetRepository
中包含这样一个方法: path =按人物查找)
页面<变更集> findByPerson(@Param(person)Person person,Pageable pageable);
(我只是从内存中执行此操作,因此您可能需要稍作调整。)
从设计的角度来看,我认为这是更强大的,因为我认为一个人的变化集在少数情况下是相关的。查询变更集而不是将其与人员捆绑在一起可能更合适。
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>();
I have a UserRepository:
@Repository
@RestResource(path = "users", rel = "users")
public interface UserRepository extends JpaRepository<User, Long>{ }
And a ChangesetRepository:
@Repository
@RestResource(path = "changesets", rel = "changesets")
public interface ChangesetRepository extends JpaRepository<Changeset, Long> { }
Calling GET on http://localhost:8080/changesets/
or http://localhost:8080/users/
yields a paged response.
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.
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.
EDIT:
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);
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.
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.
I don't think that SDR directly supports the approach you describe.
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子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!