JPA规范谓词与CriteriaQuery.any条件

时间:2016-11-24 03:03:16

标签: spring-data-jpa jpa-2.0

我有一个ClientTask对象,其中包含分配给此任务的amployee列表。

public class ClientTask {
    @OneToMany(.....)
    private List<EmployeeBean> assignedTo;
}

此onetomany关系通过表CLIENTTASKARTICLE

进行映射

我需要在哪里搜索分配给给定员工的所有任务 为此,我编写了下面给出的规范谓词。

List<Predicate> predicates = new ArrayList<Predicate>();
Root<EmployeeBean> from = criteria.from(EmployeeBean.class);
Path<Object> path = from.get("id");
final Subquery<Integer> personQuery = criteria.subquery(Integer.class);
final Root<ClientTaskBean> person = personQuery.from(ClientTaskBean.class);
final Join<ClientTaskBean, EmployeeBean> notes = person.join("assignedTo");
personQuery.select(notes.<Integer> get("id"));
predicates.add(builder.equal(builder.any(personQuery), employeeBean.getId()));
return builder.and(predicates.toArray(new Predicate[] {}));

这导致查询:

选择clienttask0_.id为id2_2_,clienttask0_.description为descript2_2_,clienttask0_.name为name3_2_,clienttask0_.status为status4_2_,clienttask0_.taskAssignment为taskAssi5_2_,clienttask0_.taskTime为taskTime6_2_,来自CLIENTTASK clienttask0_ cross join EMPLOYEE employeebe1_ where
any(从CLIENTTASK clienttask2_内部联接中选择employeebe4_.id CLIENTTASKARTICLE在clienttask2_.id上分配给_3_ = assignto3_.taskid内部联接EMPLOYEE employeebe4_ on assignedto3_.assignedTo = employeebe4_.id)= 1

所以在生成的查询中,where子句是错误的。 Where子句的格式为
“其中任何(.....子查询....)= 1”
如果我将上述查询更改为
“其中1 = any(..... subquery ....)”,它将按预期工作。

我被迫使用规范提供程序,因为可以在运行时决定更多where clase 怎么解决这个问题。请帮忙。

1 个答案:

答案 0 :(得分:0)

也许是这样的:

...builder.equal(builder.literal(employeeBean.getId()), builder.any(personQuery))...;