Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 联接中的HQL子查询_Hibernate_Hql - Fatal编程技术网

Hibernate 联接中的HQL子查询

Hibernate 联接中的HQL子查询,hibernate,hql,Hibernate,Hql,我有一个包含一组OrderSection的类Order,它本身包含一组OrderItem 在SQL中,可以在JOIN子句中使用SELECT语句,如下查询所示: SELECT o.id, o.amount, sum(s.quantity*s.price), sum(s.quantity*i.price) FROM orders AS o JOIN ordersections AS s ON s.order_id=o.id JOIN (SELE

我有一个包含一组OrderSection的类Order,它本身包含一组OrderItem

在SQL中,可以在JOIN子句中使用SELECT语句,如下查询所示:

SELECT
    o.id,
    o.amount,
    sum(s.quantity*s.price),
    sum(s.quantity*i.price)
FROM
    orders AS o
    JOIN ordersections AS s ON s.order_id=o.id
    JOIN (SELECT section_id, sum(quantity*price) AS price FROM orderitems GROUP BY section_id) AS i ON i.section_id=s.id
GROUP BY o.id, o.amount

是否可以在HQL中表达这样的查询?

除非我遗漏了什么,否则您的查询可以在SQL中重写为:

SELECT
  o.id,
  o.amount,
  sum(s.quantity*s.price),
  sum(s.quantity*i.quantity*i.price)
FROM orders AS o
  JOIN ordersections AS s ON s.order_id=o.id
  JOIN orderitems AS i ON i.section_id=s.id
GROUP BY o.id, o.amount
在这种情况下,它可以在HQL中重写为:

SELECT
  o.id,
  o.amount,
  sum(s.quantity*s.price),
  sum(s.quantity*i.quantity*i.price)
FROM orders AS o
  JOIN o.sections AS s
  JOIN s.items AS i
GROUP BY o.id, o.amount

如果我遗漏了一些东西,并且上面的查询没有返回您想要的结果,那么HQL转换就不走运了,因为。但是,您可以将查询映射为
-在末尾应该没有区别。

这不会给出相同的结果,因为第三列不依赖于项目,并且将重复每个部分中的项目。但是,将其除以count(*)就可以了,谢谢你的帮助。