![](/img/trans.png)
[英]Hibernate criteria with projection not performing query for @OneToMany mapping
[英]Convert SQL to hibernate query using criteria and/or projection (subqueries)
任何人都可以幫助將此SQL查詢轉換為HQL(使用條件和投影)以包含在結果Set<topic> topics
。
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t
JOIN posts p
ON t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;
我的問題是Hibernate正在獲取連接到topic
表的某一列(連接的表是users
)的所有數據-查詢要花費大量時間,並且不必要地進行選擇和聯接。
嘗試使用投影,但不能使用。
在此先感謝您的幫助!
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t
JOIN posts p
ON t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;
您可以刪除JOIN
並將其轉換為WHERE
條件。 希望它能拯救您。
SELECT t.id, t.title, COUNT(p.id) as count, MAX(p.created_on) as `latest`
FROM topics t, posts p
WHERE t.id = p.topic_id
GROUP BY t.id
ORDER BY count ASC;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.