Hibernate Query Exception:子查询中禁止使用Fetch问题求助
HQL查询报错:
fetch not allowed in subquery from-elements 解决方案 错误原因
Hibernate明确禁止在子查询的FROM子句中使用fetch关联。fetch的作用是立即加载关联的实体对象,而子查询仅需要返回用于筛选的字段(比如这里的taxCde),不需要加载完整实体,因此这种用法不被允许。你的错误出现在第二个not in子查询中,里面包含了多个left join fetch语句。
修复步骤
- 删除子查询里所有的
fetch关键字 - 修正子查询中表的关联顺序(原代码里
stf.stdTax在at.stdTaxForm as stf之前,属于引用未声明的别名,必须调整顺序) - 简化子查询,只保留获取
taxCde必需的关联和过滤条件
修改后的完整代码
String qw = "from StdQuestion as x" + " join fetch x.stdTaxQstns as x1" + " left join fetch x1.stdTax as x3" + " where x1.id.taxCde in (select z.taxCde from StdTax as z " + "join z.stdNaicsTaxes as z1 " + "join z1.naicsMaster as z2 " + "join z2.stdBusActivityNaicsMaps as z3 " + "join z3.stdBusinessActivity as z4 " + "where z4.businessActivityId in (:sbaId) " + "and z4.exprnDt is null " + "and z3.exprnDt is null) " + " and x.exprnDt is null" + " and x1.targetTableNm is null" + " and upper(x.rspnType) != upper('SUP') " + " and x.custTypeCde in (:custypeCode)" + " and x.qstnCategoryCde = :qstncode " + "and x1.id.taxCde not in (select st.taxCde" + " from CustSiteAcct as csa " + " left join csa.id as x2 " + " left join csa.customer as c " + " left join csa.applicableTax as at " + " left join at.stdTaxForm as stf" + " left join stf.stdTax as st " + " where csa.id.cusAcctNbr = at.acctNbr" + " and x2.cusAcctNbr = :acctNbr" + " and at.CustSiteAcct.id = x2" + " and at.acctNbr = c.acctNbr" + " and at.siteNbr = csa.id.siteNbr" + " and at.exprnDt is null" + " and c.custAcctEndDt is null" + " and csa.id.cusAcctNbr = :acctNbr)" + "order by x1.sortSeqNbr"; Query q = session.createQuery(qw); q.setParameterList("sbaId", sbaIds); q.setParameterList("custypeCode", companyType != null ? new Object[] { "ALL", companyType } : new Object[] { "ALL"} ); q.setParameter("qstncode", "T"); q.setParameter("acctNbr", acctNbr); return q.list();
关键说明
- 主查询中的
join fetch是合法的,因为主查询需要加载关联的实体对象用于后续业务逻辑 - 子查询仅需返回
taxCde字段,不需要加载任何关联实体,因此fetch完全多余,必须移除 - 调整了子查询中
stf和st的关联顺序,避免出现"未声明别名"的语法错误
内容的提问来源于stack exchange,提问作者Saber
相关产品推荐
相关产品推荐

