JPA Criteria API 包含两个列表

JPA Criteria API in with two lists

我找了一段时间没找到我想要的...

这是我的代码:

public List<MasseSalariale> findMasseSalarialeByCriteria(List<Section> sections,
  Integer exercice, List<BpCaEtats> etats) {

    List<String> etatString = new ArrayList<String>();
    for (BpCaEtats e : etats) {
      etatString.add(BpCaEtats.bpCaEtatToString(e));
    }

    CriteriaBuilder builder = this.getCriteriaBuilder();

    CriteriaQuery<MasseSalariale> query = builder.createQuery(MasseSalariale.class);
    Root<MasseSalariale> masseSalariale = query.from(MasseSalariale.class);
    Join<MasseSalariale, Etablissement> etablissement =
    masseSalariale.join(MasseSalariale_.etablissement);

    Predicate p1 = builder.equal(masseSalariale.get(MasseSalariale_.annee), exercice);

    Expression<List<Section>> exp2 = etablissement.get(Etablissement_.sections);
    Predicate p2 = exp2.in(sections);

    Expression<String> exp3 = masseSalariale.get(MasseSalariale_.etat);
    Predicate p3 = exp3.in(etatString);

    query.select(masseSalariale).where(builder.and(p1, p2, p3));

    return this.find(query);

}

基本上,我需要知道 etablissement 的部分列表中的一个(至少)值是否包含在参数的部分列表中。但是谓词 p2 我认为是错误的...

使用 ListJoin,效果很好:

ListJoin<Etablissement, Section> sectionsEtab = etablissement.join(Etablissement_.sections);
Predicate p2 = sectionsEtab.in(sections);

谢谢