I have the following 2 classes:
public class Organization extends BaseEntity implements Serializable {
...
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "organizations_players",
joinColumns = @JoinColumn(name = "organization_id"),
inverseJoinColumns = @JoinColumn(name = "player_id")
)
@ToString.Exclude
private Set<Player> players = new HashSet<>();
...
}
And class Player
in which i have not mapped the relationship. I would prefer to keep it like that if possible.
I am trying to create a Specification which will just return a list of all the players based on an Organization ID
. If I added the relationship from player -> organization this would be something like this:
public static Specification<Player> byOrganizationId(UUID id) {
return (root, query, criteriaBuilder) -> {
var organizationJoin = root.join(Player_.organizations);
return criteriaBuilder.equal(organizationJoin.get(Organization_.id), id);
};
}
Similarly in a repository
@Query("SELECT p FROM Organization o JOIN o.players p WHERE o.id = :organizationId")
List<Player> findByOrganizationId(UUID organizationId);
Is this doable? I am using specifications in order to attach additional filters dynamically, I don’t want to use a repository method.