0

I'm using Spring Data JPA to query a User entity that has a collection of UserGroups. 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?

Aron
  • 1,552
  • 1
  • 13
  • 34

1 Answers1

0

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.

Felix Seifert
  • 552
  • 1
  • 9
  • 19