1

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?

user123454321
  • 1,028
  • 8
  • 26

1 Answers1

2

An example criteria based query may look like this:

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();
wypieprz
  • 7,981
  • 4
  • 43
  • 46