3
私はJPA 2.1クライテリアAPIで、このSQLクエリを記述したいと思います:内部クエリの外部クエリのメンバを参照する方法は?
select * from t_question q
where
(select count(*) from t_question_tag tag
where
q.question_id = tag.question_id
AND tag.tag_id in (18, 1)
) = 2;
私は内側のクエリで外側質問メンバーを参照する方法を見つけ出すことはできません。
私はこの時点で、現在のよ:
CriteriaQuery<Question> cq = criteriaBuilder.createQuery(Question.class);
Root<Question> questions = cq.from(Question.class);
cq.distinct(true);
Subquery<Long> selectTags = cq.subquery(Long.class);
Root<QuestionTag> qt = selectTags.from(QuestionTag.class);
Join<QuestionTag, Question> qtJoin = qt.join("question");
selectTags
.select(criteriaBuilder.count(qtJoin))
.where(
qt.get("tag").in(filter.getTags())
);
cq.where(criteriaBuilder.and(insArray),
criteriaBuilder.equal(criteriaBuilder.literal(filter.getTags().size()), selectTags));
しかし、それは、第二の参加が作成されます。 SQLの結果は次のとおりです。
SELECT DISTINCT ...
FROM T_QUESTION question0_
WHERE 1 =
(SELECT COUNT(question3_.question_id)
FROM T_QUESTION_TAG questionta2_
INNER JOIN T_QUESTION question3_
ON questionta2_.question_id=question3_.question_id
WHERE questionta2_.tag_id IN (18));
2番目のコードフラグメントでは、 'cq'は何を表していますか? (それに応じてコード部分を更新してください) – Riduidel
そして、私はJPAがその点でどのように動作しているのか正確にはわかりませんが、あなたの 'WHERE ... = 2'は' WHERE 1 = ... ' – Riduidel