Я хочу использовать @EntityGraph с @Query, чтобы избежать проблемы n + 1.
Мой текущий метод аннотирован @Query, и в этом запросе JPA напрямую ссылается на коллекцию внутри сущности, фильтрует ее и возвращает как результат:
@Query(value = "SELECT cv "
+ "FROM CheckMatrixEntity cm "
+ "JOIN cm.checks dc "
+ "JOIN dc.checkVersions cv "
+ "where cm.channelId = :channelId and cm.clientId = :clientId and cm.branchId = :branchId "
+ "and cm.docTypeId = :docTypeId "
+ "and dc.parentCheck is null "
+ "and cv.enabled = true "
+ "and cv.startDate = (select max(cv1.startDate) from CheckVersionEntity cv1 where cv.docCheck=cv1.docCheck and cv1.startDate<=:date)")
List<CheckVersionEntity> actualEnabledChecks(@Param("docTypeId") Long docTypeId,
@Param("channelId") Long channelId,
@Param("clientId") Long clientId,
@Param("branchId") Long branchId,
@Param("date") Date date);
При добавлении @EntityGraph:
@EntityGraph(value = "CheckMatrixEntity.all", type = LOAD)
@NamedEntityGraph(
name = "CheckMatrixEntity.all",
attributeNodes = {
@NamedAttributeNode("checks")
})
public class CheckMatrixEntity implements Serializable {
Я получаю исключение:
org.springframework.dao.InvalidDataAccessApiUsageException: 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=dc,role=ru.vtb.dbo.bc.domain.db.CheckMatrixEntity.checks,tableName=bc_doc_check,tableAlias=checks1_,origin=bc_check_matrix checkmatri0_,columns={checkmatri0_.matrix_id ,className=ru.vtb.dbo.bc.domain.db.DocCheckEntity}}] [SELECT cv FROM ru.vtb.dbo.bc.domain.db.CheckMatrixEntity cm JOIN cm.checks dc JOIN dc.checkVersions cv where cm.channelId = :channelId and cm.clientId = :clientId and cm.branchId = :branchId and cm.docTypeId = :docTypeId and dc.parentCheck is null and cv.enabled = true and cv.startDate = (select max(cv1.startDate) from ru.vtb.dbo.bc.domain.db.CheckVersionEntity cv1 where cv.docCheck=cv1.docCheck and cv1.startDate<=:date)]; nested exception is java.lang.IllegalArgumentException: 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=dc,role=ru.vtb.dbo.bc.domain.db.CheckMatrixEntity.checks,tableName=bc_doc_check,tableAlias=checks1_,origin=bc_check_matrix checkmatri0_,columns={checkmatri0_.matrix_id ,className=ru.vtb.dbo.bc.domain.db.DocCheckEntity}}] [SELECT cv FROM ru.vtb.dbo.bc.domain.db.CheckMatrixEntity cm JOIN cm.checks dc JOIN dc.checkVersions cv where cm.channelId = :channelId and cm.clientId = :clientId and cm.branchId = :branchId and cm.docTypeId = :docTypeId and dc.parentCheck is null and cv.enabled = true and cv.startDate = (select max(cv1.startDate) from ru.vtb.dbo.bc.domain.db.CheckVersionEntity cv1 where cv.docCheck=cv1.docCheck and cv1.startDate<=:date)]
Но я не использую соединение извлечения.
Могу ли я искать использование внутренних коллекций в запросе, используя граф сущностей?