org.hibernate.QueryException: fetch not allowed in subquery from elements

I get an error in my console that states:

org.hibernate.QueryException: fetch not allowed in subquery from-elements

Here is the code:

public List<StdQuestion> findTaxQuestionBySba(List<Long> sbaIds, String companyType, long acctNbr) {
Session session = getSession();
		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 fetch csa.id as x2 "
		        + "    left join fetch csa.customer as c "
		        + "    left join fetch csa.applicableTax as at "
		        + "    left join fetch stf.stdTax as st "
		        + "    left join fetch at.stdTaxForm as stf"
		        + "    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();
	}

What is wrong here?

I’m sorry, but if you don’t understand this very clear exception message fetch not allowed in subquery from-elements then I don’t think anyone can help you. You have to replace left join fetch in your subquery by left join.

1 Like