2015-05-04 240 views
1

我有以下類別:JPQL到標準

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

我想創建一個使用標準,讓所有的C項,其中c.b.a.xxs

與內容SELECT c FROM C c JOIN c.b b WHERE b.a.x IN :param工作正常輸入的查詢方法getAllByXs(List<String> xs)。如何使用Criteria編寫相同的查詢?

回答

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();