[英]hql query to fetch data and sum of amount
I am writing query to fetch data from tables 我正在编写查询以从表中获取数据
my hql query is 我的hql查询是
SELECT distinct bd FROM BillDetails AS bd
LEFT JOIN FETCH bd.customerDetails AS cd
LEFT JOIN FETCH bd.billProductList AS bpd
LEFT JOIN FETCH bpd.product AS pd
WHERE bd.billNo=:id
AND bd.client.id=:cid
Above query is working properly 上面的查询工作正常
I want to write query to fetch sum of all amount field of billPaidDetailses
. 我想编写查询以获取
billPaidDetailses
的所有金额字段的billPaidDetailses
。
billPaidDetailses
is a list in BillDetails
class. billPaidDetailses
是BillDetails
类中的列表。
I am trying following query for that but it is not working 我正在尝试以下查询,但无法正常工作
String hql = "select distinct bd,sum(bpds.amount) from BillDetails as bd "
+ "left join fetch bd.customerDetails as cd "
+ "left join fetch bd.billProductList as bpd "
+ "left join fetch bpd.product as pd "
+"left join fetch bd.billPaidDetailses as bpds "
+ "where bd.billNo=:id "
+ "and bd.client.id=:cid ";
The error returned is 返回的错误是
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch
multiple bags
Quoting Hibernate manual : 引用Hibernate手册 :
Hibernate also does not currently expand a grouped entity, so you cannot write group by cat if all properties of cat are non-aggregated.
Hibernate当前也不会扩展分组的实体,因此,如果未聚合cat的所有属性,则无法按cat编写group。 You have to list all non-aggregated properties explicitly.
您必须明确列出所有未聚合的属性。
It means you will have to add all properties for all joined entities: 这意味着您将必须为所有加入的实体添加所有属性:
giving you a HQL query like: 给您一个HQL查询,例如:
select distinct bd, sum(bpds.amount)
from BillDetails as bd "
left join fetch bd.customerDetails as cd
left join fetch bd.billProductList as bpd
left join fetch bpd.product as pd
left join fetch bd.billPaidDetailses as bpds
where
bd.billNo=:id and bd.client.id=:cid
group by
bd.id,
bd.propertyA,
bd.propertyB,
cd.id,
cd.propertyC,
cd.propertyD,
pd.id,
pd.propertyE,
pd.propertyF,
bpds.id,
bpds.propertyG,
bpds.propertyH
The only advantage over a native SQL query is that Hibernate manages to regroup entities in a hierarchical structure. 相对于本机SQL查询的唯一优势在于,Hibernate可以按层次结构重新组织实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.