1

我正在创建一个繁重的 HQL 查询并尝试对其进行优化。有一个表Product,需要为每个产品计算一些统计数据,我们称之为stat。我正在尝试这种查询来一次获取所有产品及其统计信息(这是一个简化的查询,真正的查询要复杂得多):

select new map(min(product) as prod, sum(somestat) as stat) 
from Product product
  left join product.stats somestat
group by product.id, product.name
order by product.name

但是,当我尝试执行这种查询时,它首先执行主选择,然后执行 X 次SELECT product.* FROM product WHERE product.id=?选择返回的每个产品。

有没有办法让它从第一个查询中获取结果来创建那些 Product 实例?

提前致谢。

4

1 回答 1

0

If you want the whole product, then hibernate is already doing the only reasonable thing: executing N+1 selects. You happen to group by the primary key so theoretically one could imagine doing it, but even in SQL you are not allowed to select any columns not used in group-by. Anyway, such custom trickery is beyond an ORM such as Hibernate.

于 2013-01-28T15:23:33.303 回答