0

我需要形成一个 HQL 查询,该查询返回多个看起来像这样的实体对象:

/*
 * Want to return both H and C in the same query...
 */
SELECT H, C
/*
 * ... such that H is the most recent entry...
 *
 * (Note that 'ON' keyword does not work with HQL)
 */
FROM HealthHistory H INNER JOIN (
    SELECT id, MAX(insertion_time) insertion_time
    FROM HealthHistory
    GROUP BY id
) IH ON H.id = IH.id AND IH.insertion_time = H.insertion_time
/*
 * ... while getting these other entities involved...
 */
LEFT JOIN NodeA A
LEFT JOIN NodeC C
LEFT JOIN NodeL L
/*
 * ... so we can add some more expressions here...
 */
WHERE A.someId = C.descendant.id -- A -> C
  AND A.something = someConstant
  AND C.someId = L.id -- C -> L
  AND C.something = someConstant
  AND L.something = someConstant
  AND H.someId = C.id -- H -> C
  AND H.something = someConstant

我不断收到org.hibernate.hql.ast.QuerySyntaxException: unexpected token: {(, A, ON, or INNER}

即使在查看了许多类似的问题之后,我也已经挣扎了很长一段时间。对此的任何帮助表示赞赏...

4

2 回答 2

0

对于 HQL,您不需要ON ,您可以在此处找到它以获取更多信息

只要您在实体中映射它,Hibernate 就知道什么是连接列,而无需您在查询中告诉它。

于 2013-07-28T05:33:15.337 回答
0

如果有人提出上述替代方案,我会将其标记为答案。由于情况并非如此,这里是解决子查询限制和缺乏对“ON”关键字支持的解决方法:

SELECT H, C
FROM HealthHistory H,
     NodeA A,
     NodeC C,
     NodeL L
WHERE A.someId = C.descendant.id -- A -> C
  AND A.something = someConstant
  AND C.someId = L.id -- C -> L
  AND C.something = someConstant
  AND L.something = someConstant
  AND H.someId = C.id -- H -> C
  AND H.something = someConstant
  AND H.someId = C.descendant.id
  AND H.insertion_time IN (
                       SELECT MAX(insertion_time)
                       FROM HealthHistory H2
                       WHERE H2.someId = C.descendant.id
  )
于 2013-07-28T08:01:14.590 回答