我有以下两种资源及其关联;
@Table(name = "Item")
@Data
@Entity
public class Item {
@ManyToOne
@JoinColumn(name = "fk_wrapper")
private Wrapper wrapper;
@Id
@GeneratedValue
private String id;
private Integer someValue;
}
和
@Table(name = "Wrapper")
@Data
@Entity
public class Wrapper {
@Id
@GeneratedValue
private String id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_wrapper")
private List<Item> items;
private String someField;
}
然后,首先,我创建一个Wrapper
;
POST http://localhost:8080/wrappers/
{
"someField": "asd"
}
http://localhost:8080/wrappers/1
已创建,然后我创建了两个Item
,链接到此Wrapper
;
POST http://localhost:8080/items/
{
"someValue": "5",
"wrapper": "http://localhost:8080/wrappers/1"
}
&安培;
POST http://localhost:8080/items/
{
"someValue": "7",
"wrapper": "http://localhost:8080/wrappers/1"
}
毕竟,当我调用端点http://localhost:8080/wrappers/1/items
时,我会按预期获得这两个项目的列表,但问题是,我似乎无法在此端点上使用排序功能。我似乎能够在http://localhost:8080/items
端点中进行排序,但在使用关联获取时,似乎没有排序功能。是否缺乏分类,或者我缺少一些配置?
P.S。当我创建自定义搜索方法时,例如;
@RepositoryRestResource
public interface ItemRepository extends JpaRepository<Item, String> {
List<Item> findByWrapper_Id(@Param("id") String id, Sort sort);
}
然后我可以使用http://localhost:8080/items/search/findByWrapper_Id
端点进行排序,但是考虑到已经有一个自动生成的端点,它太丑了。
答案 0 :(得分:1)
Spring Data Rest不支持对关联进行排序。
根据Spring Data Rest团队的说法,您似乎已经找到了满足您需求的最佳方式 - 创建查询以获取所需的数据。这确实会支持分页和排序。
不支持它的原因与查询获取主资源的时间(构建关联端点之前)以及关联端点使用加载的事实有关实体关联直接和支持排序,无论如何都需要进行新的查询。
更多详细信息可在此处找到:
干杯!