我选择了两个id列,但指定了错误:
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=r,role=null,tableName=REVISIONS,tableAlias=revision1_,origin=ENTITY_CHANGED_IN_REVISION entitychan0_,columns={entitychan0_.REV_ID ,className=ru.csbi.registry.domain.envers.Revision}}] [ select ec.id as entityChangeId, r.id as revisionId from ru.csbi.registry.domain.envers.EntityChange as ec inner join fetch ec.revision as r where ec.groupEntityId = :groupEntityId and ec.groupName = :groupName and r.timestamp < :entityDateFrom and r.timestamp > :entityDateTo and ( ec.revisionType in (0, 5, 1, 4, 2 ) and not ( ec.otherGroupEntityModified = false and ec.thisGroupEntityModified = true and ec.rowDataModified = false and ec.collectionOfNotGroupEntityModified = false ) ) group by ec.id, r.id having count(*) > :start order by r.id desc]
一些代码:
String hql = " select ec.id as entityChangeId, r.id as revisionId from EntityChange as ec " +
" inner join fetch ec.revision as r " +
" where ec.groupEntityId = :groupEntityId" +
" and ec.groupName = :groupName " +
" and r.timestamp < :entityDateFrom " +
" and r.timestamp > :entityDateTo " +
" and ( " +
" ec.revisionType in (" +
RevisionType.ADD.getRepresentation() + ", " +
RevisionType.ONLY_DATA_PROPERTY_MOD.getRepresentation() + ", " +
RevisionType.BOTH_COLLECTION_AND_PROPERTY_MOD.getRepresentation() + ", " +
RevisionType.ONLY_COLLECTION_PROPERTY_MOD.getRepresentation() + ", " +
RevisionType.DEL.getRepresentation() +
" ) " +
" and not ( "+
"ec.otherGroupEntityModified = false and " +
"ec.thisGroupEntityModified = true and " +
"ec.rowDataModified = false and " +
"ec.collectionOfNotGroupEntityModified = false " +
" ) " +
" ) " +
" group by ec.id, r.id " +
" having count(*) > :start" +
" order by r.id desc";
如何修复错误,我做错了什么?
6条答案
按热度按时间6jjcrrmo1#
使用常规的
join
而不是join fetch
(顺便说一下,默认情况下是inner
):正如错误消息告诉您的,
join fetch
在这里没有意义,因为它是一个性能提示,强制立即加载集合。qojgxg4l2#
由于您需要
join fetch
,因此删除fetch
无法满足您的需求。您应该做的是将计数查询与它一起指定。
假设您正在对结果进行分页,下面是一个JPA查询,它将id作为
param
,并将导致您指定的问题,第二个查询通过添加count查询来解决这个问题。注意:
fk_field
是tableA
中具有 one-to-many rln的属性。计数查询不使用join fetch
。dvtswwa33#
与OP的特定查询无关,但对我来说,由于嵌套关系的
JOIN FETCH
语法不正确,因此抛出了相同的query specified join fetching, but the owner of the fetched association was not present in the select list
错误,必须通过别名引用,而我直观地尝试通过完整路径引用它。错误:(导致
not present in the select list
错误)Good:(works!)
希望这能帮到别人
sg2wtvxw4#
这个问题与Spring Data上的N+1问题有关,如果您想使用JpaSpecificationExecutor,您可以解决它,您可以将 Page 更改为 Slice,请查看此输入:Link
kognpnkq5#
必须将相关项目列放入选择子句中
3duebb1j6#
如果您使用的是SpringJPA,那么这个查询应该可以为您修复它
@Query( value = “FROM TableName t LEFT JOIN FETCH t.columnName WHERE t.columnName = value”, countQuery = “select count(t) from TableName t left join t.columnName WHERE t.columnName = value”)
请注意,where子句可以被删除,因为您不知道过滤依据是什么