JPA Criteria API包含两个列表

时间:2015-06-23 08:02:25

标签: java hibernate jpa criteria builder

我一直在寻找一段时间而且我找不到我想要的东西......

这是我的代码:

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的section list中的一个(至少)值是否包含在参数的section list中。但是我认为谓词p2是错误的......

1 个答案:

答案 0 :(得分:1)

使用ListJoin,效果很好:

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

由于