簡體   English   中英

通過JPA2多次加入

[英]Multiple Joins WIth 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書籍,看看是否可以解決這個問題!

我認為應該是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM