JPQL to Criteria

时间:2015-05-04 19:40:53

标签: java jpa criteria jpql

I have following classes:

@Embeddable Class A // (with field String x);
Class B // (with field @Embedded A a)
Class C // (with field @OneToOne B b);

I would like to create method getAllByXs(List<String> xs) using criteria to get all C entries where c.b.a.x is in xs

Typed query with content SELECT c FROM C c JOIN c.b b WHERE b.a.x IN :param works fine. How to write same query using Criteria?

1 个答案:

答案 0 :(得分:2)

基于标准的示例查询可能如下所示:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<C> cq = cb.createQuery(C.class);
Root<C> c = cq.from(C.class);
Join<C, B> b = c.join("b");
cq.select(c)
cq.where(b.get("a").get("x").in(cb.parameter(List.class, "param")));

List<C> = em.createQuery(cq)
            .setParameter("param", Arrays.asList("foo", "bar", "baz"))
            .getResultList();