Spring Data Rest 可分页子集合 [英] Spring Data Rest Pageable Child Collection

查看:27
本文介绍了Spring Data Rest 可分页子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 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.

不过,您还可以采用另一种方法.与其在 PersonChangeset 之间使用双向关系,不如让它从 ChangesetPerson 是单向的.然后在您的 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 可分页子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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