写一个hql,其中用到了 left join fetch ,最后报错:
2021-02-23 09:23:24,911 ERROR [qtp383124839-17] dispatcher.DefaultDispatcherErrorHandler (CommonsLogger.java:42) - Exception occurred during processing request: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=cb_ba_BANK_ACCOUNT,tableAlias=cbbankacco1_,origin=cb_ba_withdraw withdraw0_,colums={withdraw0_.accountId_ ,className=com.skysz.app.cb.bank.domain.CbBankAccount}}] [select w.id, w.status, w.message, w.submitTime, w.createBy.name as user, w.businessNO, w.account.accountNO from com.skysz.app.cb.withdraw.domain.WithDraw w left join fetch w.account left join w.createBy where 1=1 and to_char(w.submitTime,'yyyy-mm-dd') = ?]
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=cb_ba_BANK_ACCOUNT,tableAlias=cbbankacco1_,origin=cb_ba_withdraw withdraw0_,colums={withdraw0_.accountId_ ,className=com.skysz.app.cb.bank.domain.CbBankAccount}}] [select w.id, w.status, w.message, w.submitTime, w.createBy.name as user, w.businessNO, w.account.accountNO from com.skysz.app.cb.withdraw.domain.WithDraw w left join fetch w.account left join w.createBy where 1=1 and to_char(w.submitTime,'yyyy-mm-dd') = ?]
原语句
select w.id, w.status, w.message, w.submitTime, w.createBy.name as user, w.businessNO, w.account.accountNO from WithDraw w left join fetch w.account left join w.createBy where 1=1
原因:select后面不是对象,用了w.status去部分字段,所以不能用 left join fetch
下面语句正常。
from WithDraw w left join fetch w.account left join fetch w.createBy where 1=1
总结:
1、select后面不是对象,用了w.status 去部分字段就不能用 left join fetch
2、取对象,不取部分字段,可以使用left join fetch