JPA Criteria API in with two lists

2.2k views Asked by At

I've been searching for a while and I don't find what i want...

Here is my code :

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);

}

Basically, I need to know if one (at least) value in the section list from etablissement is contained in the section list in parameter. But the predicate p2 is wrong I think...

1

There are 1 answers

0
CrowMash On BEST ANSWER

Using ListJoin, it worked well :

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

Thanks