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

后端 未结 2 998
不知归路
不知归路 2020-12-14 13:33

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

org.hibernate.QueryException: **query specified join fetching, but the owner of the fetched associatio         


        
2条回答
  •  再見小時候
    2020-12-14 14:25

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

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

    Assuming you are paginating the result, below is jpa query that takes id as 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")
    

提交回复
热议问题