Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query specified join fetching, but the owner of the fetched association was not present in the select list

I'm selecting two id columns but get error specified:

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]

Some code:

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";

How to fix the error and what am I doing wrong?

like image 553
Vyacheslav Avatar asked Sep 17 '12 13:09

Vyacheslav


4 Answers

As you need the join fetch, removing the fetch won't meet your needs.

What you should do instead is specify a count query together with it.

Assuming you are paginating the result, below is a JPA query that takes id as a param and will cause the problem you specified and the second query solves this by adding count query to it.

Note: fk_field is the attribute in tableA that has the one-to-many rln. The count query does not use 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")
       
like image 20
mykey Avatar answered Oct 29 '22 05:10

mykey


Use regular join instead of join fetch (by the way, it's inner by default):

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

As error message tells you, join fetch doesn't make sense here, because it's a performance hint that forces eager loading of collection.

like image 153
axtavt Avatar answered Oct 29 '22 04:10

axtavt


Not relevant to OP's specific query, but for me same query specified join fetching, but the owner of the fetched association was not present in the select list error was thrown due to incorrect JOIN FETCH syntax of a nested relation, it must be referenced via an alias whereas I intuitively tried to refer to it via full path.

Bad: (causes not present in the select list error)

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

Hope that helps someone

like image 3
Klesun Avatar answered Oct 29 '22 03:10

Klesun


You have to put your related item column into selection clause

select ec.id as entityChangeId, r.id as revisionId, **r.revision** 
from EntityChange as ec " +
        " inner join fetch ec.revision as r " +
like image 1
Dmitry Avatar answered Oct 29 '22 05:10

Dmitry