I'm using Spring Data JPA to query a User
entity that has a collection of UserGroup
s. The ordering of these groups is significant, so I've declared an @OrderColumn
on the relationship:
@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. I have this query method in my 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?
You should provide a column name which should be used for ordering the relationship. This answer shows how the annotation @OrderColumn
could be used. Even though the example is for a @OneToMany
relationship, this ordering should also work for @ManyToMany
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.