select ptm.* from ProofTestMaster ptm LEFT JOIN
ProofTestApplicationMap ptam on ptm.proofTestID = ptam.proofTestID
LEFT JOIN ProofTestComapartmentMap ptcm on ptm.proofTestID = ptcm.proofTestID
where (ptam.applicationID = 3 and ptm.isDeleted = 0) or
(ptcm.compartmentID = 4 and ptm.isDeleted = 0)
ProofTestApplicationMap
和ProofTestComapartmentMap
是映射表,并且它们在java端没有实体。
答案 0 :(得分:1)
最好为查询添加适当的缩进。优点是:
所以我为你做了这个。现在查询看起来像:
select
ptm.*
from ProofTestMaster ptm
LEFT JOIN ProofTestApplicationMap ptam on ptm.proofTestID = ptam.proofTestID
LEFT JOIN ProofTestComapartmentMap ptcm on ptm.proofTestID = ptcm.proofTestID
where
(ptam.applicationID = 3 and ptm.isDeleted = 0) or
(ptcm.compartmentID = 4 and ptm.isDeleted = 0);
下面是CriteriaBuilder与Static Metamodel的实现:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<ProofTestMaster> mainRoot = criteriaQuery.from(ProofTestMaster.class);
Join<ProofTestMaster, ProofTestApplicationMap> firstJoin = mainRoot.join(ProofTestMaster_.proofTestID, JoinType.LEFT);
Join<ProofTestMaster, ProofTestComapartmentMap> secondJoin = mainRoot.join(ProofTestMaster_.proofTestID, JoinType.LEFT);
Predicate p1 = criteriaBuilder.equal(firstJoin.get(ProofTestApplicationMap_.applicationID),3);
Predicate p2 = criteriaBuilder.equal(mainRoot.get(ProofTestMaster_.isDeleted),0);
Predicate p3 = criteriaBuilder.equal(secondJoin.get(ProofTestComapartmentMap_.compartmentID), 4);
Predicate p4 = criteriaBuilder.and(p1,p2);
Predicate p5 = criteriaBuilder.and(p3,p2);
Predicate p6 = criteriaBuilder.or(p4,p5);
criteriaQuery.where(p6);
criteriaQuery.select(criteriaBuilder.count(mainRoot));
Long count = entityManager.createQuery(criteriaQuery).getSingleResult();
如果你看到上面的代码,总共有6个Predicates,可以放入List。但是为了你的理解我保持这样。
请告诉我它是否对您有所帮助。谢谢,快乐的编码。