我在两个条件之间创建交集(和)的查询不起作用,为什么? (休眠,Java,休息应用程序)

My query to create an intersection (and) between two conditions doesn't work, why ? (Hibernate, Java, Rest Application)

@GET
@Path("/colors/{params: .*}")
@Produces(MediaType.APPLICATION_JSON)
public List<Espece> findByColors(@PathParam("params") List<PathSegment> params){
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<Espece> cq = cb.createQuery(Espece.class);
    Root<Espece> birdType = cq.from(Espece.class);
    Join<Espece, Couleur> couleurs = birdType.join(Espece_.colors);

    List<Predicate> restrictions = new ArrayList<>();
    for(PathSegment param : params){
        restrictions.add(cb.equal(cb.lower(couleurs.get(Couleur_.nom)), param.toString().toLowerCase()));
    }
    cq.where(cb.and(restrictions.toArray(new Predicate[]{})));
    /*cq.select(birdType)
        .where(restrictions.toArray(new Predicate[]{}));*/

    return getEntityManager().createQuery(cq).getResultList();
}

我的方法是用一种或多种颜色搜索一只鸟(一只鸟可以有不止一种颜色)。当我只使用一种颜色进行搜索时:http://localhost:8084/NataRest/bird/colors/black 它工作正常。 但是当我用两种颜色搜索时:http://localhost:8084/NataRest/bird/colors/black/yellow 它给了我一个空答案(是的,我的数据库中有一些鸟是黑色和黄色的)。 我检查了多个论坛,但仍然没有找到使这项工作正常进行的方法...我可以使用 'or' 和 'distinct' 进行合并。但不是我想要的十字路口:/

感谢您的提前帮助! 好的。

为了解决我的问题,我编写了 MySql 代码来完成我想做的事情。 它让我大开眼界,我想要 a = "yellow" 和 a = "black",所以这是不可能的。 这是我构建的解决方案。 创建查询列表(每种颜色一个查询)并在这些查询的结果之间建立交集。

@GET
@Path("/colors/{params: .*}")
@Produces(MediaType.APPLICATION_JSON)
public List<Espece> findByColors(@PathParam("params") List<PathSegment> params){
    List<Query> queries = new ArrayList<>();
    for(PathSegment param : params){
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Espece> cq = cb.createQuery(Espece.class);
        Root<Espece> birdType = cq.from(Espece.class);
        Join<Espece, Couleur> couleurs = birdType.join(Espece_.colors);
        cq.where(cb.equal(cb.lower(couleurs.get(Couleur_.nom)), param.getPath().toLowerCase()));
        queries.add(getEntityManager().createQuery(cq));
    }

    List<Espece> result = queries.get(0).getResultList();
    for(int i = 1; i < queries.size(); i++){
        result.retainAll(queries.get(i).getResultList());
    }

    return result;
}