hibernate 查询指定了联接提取,但提取的关联的所有者不在选择列表中

qnyhuwrf  于 2023-05-07  发布在  其他
关注(0)|答案(6)|浏览(144)

我选择了两个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";

如何修复错误,我做错了什么?

6jjcrrmo

6jjcrrmo1#

使用常规的join而不是join fetch(顺便说一下,默认情况下是inner):

String hql = " select ec.id as entityChangeId, r.id as revisionId from EntityChange as ec " + 
        " join ec.revision as r " + ...

正如错误消息告诉您的,join fetch在这里没有意义,因为它是一个性能提示,强制立即加载集合。

qojgxg4l

qojgxg4l2#

由于您需要join fetch,因此删除fetch无法满足您的需求。
您应该做的是将计数查询与它一起指定。
假设您正在对结果进行分页,下面是一个JPA查询,它将id作为param,并将导致您指定的问题,第二个查询通过添加count查询来解决这个问题。
注意:fk_fieldtableA中具有 one-to-many rln的属性。计数查询不使用join fetch

@Query(value = "from TableA a LEFT JOIN FETCH a.fk_field where a.id = :id") 

@Query(value = "from TableA a LEFT JOIN FETCH a.fk_field where a.id = :id", 
  countQuery = " select  count(a) from TableA a left join a.fk_field where a.id = :id")
dvtswwa3

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错误)

SELECT * FROM TableA a
JOIN FETCH a.relationB
JOIN FETCH a.relationB.relationC

Good:(works!)

SELECT * FROM TableA a
JOIN FETCH a.relationB AS b
JOIN FETCH b.relationC

希望这能帮到别人

sg2wtvxw

sg2wtvxw4#

这个问题与Spring Data上的N+1问题有关,如果您想使用JpaSpecificationExecutor,您可以解决它,您可以将 Page 更改为 Slice,请查看此输入:Link

kognpnkq

kognpnkq5#

必须将相关项目列放入选择子句中

select ec.id as entityChangeId, r.id as revisionId, **r.revision** 
from EntityChange as ec " +
        " inner join fetch ec.revision as r " +
3duebb1j

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子句可以被删除,因为您不知道过滤依据是什么

相关问题