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

您如何制定此标准查询以匹配此 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
)
4

1 回答 1

0

我为谓词分配了错误的对象。如果要将子查询的 id 与父 id 匹配,则必须分配Join, 而不是Root, if 谓词不属于根。嗯,这是多么明显。:) 但是,很高兴分享。下面是新的查询。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();
于 2013-09-06T10:42:02.787 回答