【问题标题】:Convert SQL to hibernate query using criteria and/or projection (subqueries)使用条件和/或投影(子查询)将 SQL 转换为休眠查询
【发布时间】:2016-12-05 01:03:18
【问题描述】:

任何人都可以帮助将此 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) - 查询需要很多时间和不必要的选择和连接。
尝试使用投影但不能。
提前感谢您的帮助!

【问题讨论】:

    标签: java mysql hibernate criteria projection


    【解决方案1】:

    根据您的代码,

    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;
    

    【讨论】:

    • 问题是我需要带有标准和预测的 hql 查询。
    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多