[英]Spring Data JPA sort by join table @OrderColumn?
I'm using Spring Data JPA to query a User
entity that has a collection of UserGroup
s.我正在使用 Spring Data JPA 来查询具有
UserGroup
集合的User
实体。 The ordering of these groups is significant, so I've declared an @OrderColumn
on the relationship:这些组的顺序很重要,所以我在关系上声明了一个
@OrderColumn
:
@Entity
public class User {
@Id
private String id;
@ManyToMany
@OrderColumn
private List<UserGroup> groups;
}
I need to query the groups for a particular user, and I want them to come back in the order specified by the @OrderColumn
in the join table.我需要查询特定用户的组,并且我希望它们按照连接表中
@OrderColumn
指定的顺序返回。 I have this query method in my UserGroupRepository
.我的
UserGroupRepository
中有这个查询方法。
public Page<UserGroup> findByUsersIdContains(String userId, Pageable pageable);
That query does not honor the ordering I want.该查询不遵守我想要的顺序。 Is there something I can add to the method name, or specifying in the
Sort
definition within the Pageable
, to order by the order column in the join table?我可以在方法名称中添加什么内容,或者在
Pageable
的Sort
定义中指定,以按连接表中的 order 列排序吗?
You should provide a column name which should be used for ordering the relationship.您应该提供一个列名,用于对关系进行排序。 This answer shows how the annotation
@OrderColumn
could be used.这个答案显示了如何使用注释
@OrderColumn
。 Even though the example is for a @OneToMany
relationship, this ordering should also work for @ManyToMany
.即使该示例是针对
@OneToMany
关系的,这种排序也应该适用于@ManyToMany
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.