为什么JPA Criteria Query不允许关联子查询?

时间:2013-09-06 09:17:57

标签: java sql jpa-2.0

我需要将主查询与具有类别ID的子查询匹配。 JPA Criteria Query不允许我将Predicate设置为Category,因为查询返回类型PubThread

Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);

低于整个条件查询。

   CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
   CriteriaQuery<PubThread> cq = criteriaBuilder.createQuery(PubThread.class);

    // 1) MainQuery
    // Create the FROM
    Root<PubThread> rootPubThread = cq.from(PubThread.class);
    // Create the JOIN from the first select: join-chaining. You only need the return for ordering. e.g. cq.orderBy(cb.asc(categoryJoin.get(Pub_.title)));
    Join<Pub, PubCategory> categoryJoin = rootPubThread.join(PubThread_.pups).join(Pub_.pubCategory);
    // Create the WHERE
    cq.where(criteriaBuilder.not(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId)));
    // Create the SELECT, at last
    cq.select(rootPubThread).distinct(true);

    // 2) Subquery
    Subquery<PubThread> subquery = cq.subquery(PubThread.class); 
    Root<PubThread> rootPubThreadSub = subquery.from(PubThread.class); 
    subquery.where(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId));
    Join<Pub, PubCategory> categoryJoinSub = rootPubThreadSub.join(PubThread_.pups).join(Pub_.pubCategory);
    subquery.select(rootPubThreadSub);

    Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);
    subquery.where(correlatePredicate);
    cq.where(criteriaBuilder.exists(subquery));

如何制定此Criteria Query以匹配此SQL代码?我觉得我很亲密。

SELECT Distinct(pt2.id), pt2.name
FROM pubthread pt2
JOIN pub_pubthread ppt2 ON pt2.id = ppt2.pubThreads_id
JOIN pub p2 ON ppt2.pups_id = p2.id
JOIN pubcategory pc2 ON p2.pubCategoryId = pc2.id
WHERE pt2.id != 1 and EXISTS (
    SELECT DISTINCT(pt.id) FROM pubthread pt
    JOIN pub_pubthread ppt ON pt.id = ppt.pubThreads_id
    JOIN pub p ON ppt.pups_id = p.id
    JOIN pubcategory pc ON p.pubCategoryId = pc.id
    where pc2.id = pc.id and pt.id = 1
)

1 个答案:

答案 0 :(得分:0)

我将错误的对象分配给谓词。如果要将子查询的ID与父ID匹配,则必须分配Join,而不是Root,如果谓词不属于根。嗯,这有多明显。 :)但是,很高兴分享。以下是新查询。最底部还有一个Predicate,可以解决问题。还与另一个布尔语句结合使用。

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();         CriteriaQuery mainQuery = criteriaBuilder                 .createQuery(PubThread.class);

// 1) MainQuery
// Create the FROM
Root<PubThread> rootPubThread = mainQuery.from(PubThread.class);
// Create the JOIN from the first select: join-chaining. You only need the return for ordering. e.g. cq.orderBy(cb.asc(categoryJoin.get(Pub_.title)));
Join<Pub, PubCategory> categoryJoin = rootPubThread.join(PubThread_.pups).join(Pub_.pubCategory);
// Create the WHERE
mainQuery.where(criteriaBuilder.not(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId)));
// Create the SELECT, at last
mainQuery.select(rootPubThread).distinct(true);

// 2) Subquery
Subquery<PubThread> subquery = mainQuery.subquery(PubThread.class); 
Root<PubThread> rootPubThreadSub = subquery.from(PubThread.class); 
//subquery.where(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId));
Join<Pub, PubCategory> categoryJoinSub = rootPubThreadSub.join(PubThread_.pups).join(Pub_.pubCategory);
subquery.select(rootPubThreadSub);

//Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);
Predicate correlatePredicate = criteriaBuilder.and(
        //criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread),
        criteriaBuilder.equal(categoryJoinSub.get(PubCategory_.id), categoryJoin.get(PubCategory_.id)),

        criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), threadId)
        );
subquery.where(correlatePredicate);     

//Predicate correlatePredicate = criteriaBuilder.equal(rootPubThreadSub.get(PubThread_.id), rootPubThread);
Predicate mainPredicate = criteriaBuilder.and(
        criteriaBuilder.not(criteriaBuilder.equal(rootPubThread.get(PubThread_.id), threadId)),
        criteriaBuilder.exists(subquery)
        );
//cq.where(criteriaBuilder.exists(subquery));
mainQuery.where(mainPredicate);


TypedQuery<PubThread> typedQuery = em.createQuery(mainQuery);
List<PubThread> otherPubThreads = typedQuery.getResultList();