0

私はJPA2を理解しようとしており、1つの結果を得るためにいくつかの結合を実行しようとしています。これが私が現時点で試したことです:

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呼び出しの2番目と3番目のパラメーターをコメントアウトすると、機能します(したがって、顧客名のみを提供します)。だから私は何かがおかしい。何がわからない!これが私が達成しようとしているクエリであり、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の本に戻って、これを理解できるかどうかを確認します。

4

1 に答える 1

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)
));
于 2012-05-18T14:36:46.550 に答える