2012-05-18 2 views
0

JPA2 그립을 시도하고 몇 가지 조인을 시도하는 하나의 결과를 얻으려고. 여기에 내가 지금 시도한 바가있다 :여러 조인 JPA2

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); 
CriteriaQuery<FileCollection> cq = cb.createQuery(getEntityClass()); // getEntityClass() will return FileCollection.class 

Root<FileCollection> collectionRoot = cq.from(getEntityClass()); 
Join<FileCollection, Repository> repositories = collectionRoot.join(FileCollection_.repository); 
Join<Repository, Customer> customers = repositories.join(Repository_.customer); 

cq.select(collectionRoot); 
cq.where(cb.equal(customers.get(Customer_.name), customerName), 
    cb.equal(repositories.get(Repository_.name), repositoryName) , 
    cb.equal(collectionRoot.get(FileCollection_.folderName), folderName) 
); 

return getEntityManager().createQuery(cq).getSingleResult(); 

이것은 작동하지 않는다. 두 번째 & where 매개 변수의 세 번째 매개 변수를 주석 처리하면 작동합니다 (고객 이름 만 제공 할 것입니다). 그래서 나는 잘못된 것을 얻고있다. 나는 단지 무엇이 있는지 모른다! 다음은 SQL로 표현하고자하는 쿼리입니다.

SELECT f.* 
FROM filecollection f 
JOIN repository r ON f.REPOSITORY_ID = r.REPOSITORY_ID 
JOIN customer c ON r.CUSTOMER_ID = c.CUSTOMER_ID 
WHERE c.NAME = 'X' AND r.NAME = 'Y' AND f.FOLDER_NAME = 'Z'; 

누구든지 나를 도와 주며 실수를 지적 할 수 있습니까? 그 사이에 JPA2 책으로 돌아가서 내가 이것을 알아낼 수 있는지 알아볼 것입니다!

+0

모든 예외 스택 추적? – DaTroop

+0

NoResultsException입니다. 전달하는 매개 변수는 정확하지만 내 쿼리가 정확하면 결과를 얻을 것으로 기대합니다. –

답변

1

나는 그것을 같이해야한다고 생각 :

cq.where(cb.and(cb.equal(customers.get(Customer_.name), customerName), 
       cb.equal(repositories.get(Repository_.name), repositoryName) , 
       cb.equal(collectionRoot.get(FileCollection_.folderName), folderName) 
)); 
+0

결과가 변경되지 않았습니다. 내가해서는 안되는 결과는 여전히 결과를 반환하지 않습니다. –

+0

결과 SQL 쿼리는 어떻게 생겼습니까? – DaTroop

+0

난 그냥 최대 절전 모드 로깅 켜져 그리고 나는 내 실수를 발견했다. 귀하의 솔루션은 정확합니다 - 나는 단지 입력 할 수 없습니다! 도움에 감사드립니다 : –