如何使用jpa进行多对多查询,条件列表必须全部为true



我有两个实体User和Prestation,它们具有多对多关系。我想让所有用户都有特定的Prestation,比如ID Prestation 的列表

我的查询:

@Query("SELECT distinct u FROM User u JOIN u.prestation p where p.id In (:prestationsList)")
public List<User> findAllUser(@Param("prestationsList") List<Integer> prestationsList);

但这并没有给我正确的数据,因为(In(返回用户,如果我的prestionList的一个元素是真的

我的用户:

@Entity
public class User {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "users_prestations",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "prestation_id")})
private Set<Prestations> prestation = new HashSet<>();
}

我的Prestations:

@Entity
public class Prestations {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "prestation")
private Set<User> user = new HashSet<>();
}

我应该如何用JPA编写这个查询?

尝试使用此JPQL查询

@Query("SELECT * FROM User u left join u.prestation p where p.id In (:prestationsList)")
public List<User> findAllUser(@Param("prestationsList") List<Integer> prestationsList);

如果这不起作用,请尝试本机查询

最新更新