I would like to query for Roles with Assignments which has date_to equal null in one query. I need all roles returned even those without assigned assignments.
Relation in Role entity is set:
@OneToMany(mappedBy = "role") private List<Assignment> assignment; Assignment Entity has relation to Permission:
@ManyToMany @JoinTable( name = "assignment_version_permission", joinColumns = @JoinColumn(name = "assignment_version_id"), inverseJoinColumns = @JoinColumn(name = "permission_id")) private Set<Permission> permissions; @Column(name = "date_to") private LocalDateTime dateTo; and from Permission entity:
@ManyToMany @JoinTable( name = "assignment_version_permission", joinColumns = @JoinColumn(name = "permission_id"), inverseJoinColumns = @JoinColumn(name = "assignment_version_id")) @ToString.Exclude private List<Assignment> assignments; I tried to query for all roles and their assignments ONLY where date_to equal null.
@Query("from Role r left join Assignment av on r.dbid = av.role.dbid and av.dateTo is null left join av.permissions p where r.name in :names") List<Role> readByRoleNameInOnlyActiveAssignments (List<String> names); The above query works, but after role.getAssignemts () I would only like to receive assignments where date_to is null or null if there are no assignments.
Now role.getAssignemts() does an additional query for all Assignments related to Role. When a role has no active Assignments, I don't want to receive any of them. How can I achieve it?
I tried with EntityGraph but I there is no option to add contition on assignemns (return only these where assignment date_to is null)
@EntityGraph(attributePaths = {"assignments", "assignments.permissions"}) List<Role> readByNameIn(List<String> names);