두 개의 Entities Cats and Licks가 있다고 가정 해 보겠습니다. 각각에는 다른 하나의 목록이 있습니다. JPA를 써서 다른 사람의 ID를 얻으면 어떻게 찾을 수 있습니까?JPA를 사용하여 목록에 가입하는 방법
내 질문은 이것입니다. 더 간단하게 만들지 않겠습니까? 이 인터페이스는 혼란 스럽습니다 ...하지만 어쨌든 당신이 그것을하는 방법입니다.
@PersistenceContext
protected EntityManager em;
@Test
public void testFindCatsByLickId() throws Exception
{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Cat> cats = cq.from(Cat.class);
Root<Lick> licks = cq.from(Lick.class);
ListJoin<Cat, Lick> joinCatL = cats.join(Cat_.lickList);
// just creating the following ListJoin object will cause this query to fail!
// ListJoin<Lick, Cat> joinLCat = lick.join(Lick_.catList);
Predicate p = cb.and(
cb.equal(licks.get(Lick_.lickId), new Integer(2))
, cb.equal(licks, joinCatL)
);
cq.select(cats).where(p);
TypedQuery query = em.createQuery(cq);
List<Cat> list = query.getResultList();
assertList(list);
assertTrue(null != list && ! list.isEmpty());
}
대기 당신은 질문을하거나 자신의 대답입니까? 나는 이것이 무엇을하고 있는지 혼란 스럽다. – Woot4Moo