For now, I have a Repository class, that has a Department and a list of Users. For a connected user, I want to retrieve all repositories from a dedicated department or repositories where the user is listed.
Here is how it is managed using Spring Data query:
@Query(value = "select distinct repository " +
"from Repository repository " +
"left join repository.users " +
"where analysis.department =:department " +
"or :connectedUser member of repository.users ",
countQuery = "select count(distinct repository) from Repository repository")
Page<Analysis> findAllByUser(Pageable pageable,
@Param("department") Department department,
@Param("connectedUser") User connectedUser);
It is working well.
I have now to integrate a new feature. The repository can be linked to a list of UserGroup.
I would like to update my method with something like this:
@Query(value = "select distinct repository " +
"from Repository repository " +
"left join repository.users " +
"left join repository.userGroups " +
"where analysis.department =:department " +
"or :connectedUser member of repository.users " +
"or (at least one element of :userGroupList) member of repository.userGroups ",
countQuery = "select count(distinct repository) from Repository repository")
Page<Analysis> findAllByUser(Pageable pageable,
@Param("department") Department department,
@Param("connectedUser") User connectedUser,
@Param("userGroupList") Set<UserGroup> userGroupList;
Is there a way to do that? If so, what should I use instead of (at least one element of userGroupList)?
Maybe, member of is not the best approach... If so, could you advise another one? My main objective is to be able to manage that in a single query to get page results.
Thanks in advance for all help and advice you could provide! :)
If it can help, here are entities used (high level, not in detail):
public class Repository {
private String name;
private Department department;
@ManyToMany
private Set<User> users = new HashSet<>();
@ManyToMany
private Set<UserGroup> userGroups = new HashSet<>();
public class UserGroup {
private String name;
private Department department;
private Set<User> users = new HashSet<>();