具有分页的Spring-Data FETCH JOIN不起作用

时间:2022-09-11 16:22:54

I am trying to use HQL fetching my entity along with sub-entities using JOIN FETCH, this is working fine if I want all the results but it is not the case if I want a Page

我正在尝试使用HQL使用JOIN FETCH获取我的实体和子实体,如果我想要所有结果,这可以正常工作但如果我想要一个页面则不是这样

My entity is

我的实体是

@Entity
@Data
public class VisitEntity {

    @Id
    @Audited
    private long id;

    .
    .
    .   

    @OneToMany(cascade = CascadeType.ALL,)
    private List<VisitCommentEntity> comments;
}

and because I have millions of visits I need to use Pageable and I want to Fetch the comments in a single database query like :

因为我有数百万次访问,我需要使用Pageable,我想在单个数据库查询中获取注释,如:

@Query("SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ..." )
public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
        Pageable pageable);

That HQL call throws the following exception:

该HQL调用抛出以下异常:

Caused by: 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=null,role=com.ro.lib.visit.entity.VisitEntity.comments,tableName=visitdb.visit_comment,tableAlias=comments1_,origin=visitdb.visit visitentit0_,columns={visitentit0_.visit_id ,className=com.ro.lib.visit.entity.VisitCommentEntity}}] [select count(v) FROM com.ro.lib.visit.entity.VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and (v.actualArrival > :date or v.arrival > :date)]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:309)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

and once I remove the paging everything works fine

一旦我删除了分页,一切正常

@Query("SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and  ..." )
public List<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...);

Obviously the problem is the count query from Spring-Data, but how can we fix it?

显然问题是来自Spring-Data的count查询,但是我们如何解决呢?

3 个解决方案

#1


39  

The easiest way is to use the countQuery attribute of the the @Query annotation to provide a custom query to be used.

最简单的方法是使用@Query批注的countQuery属性来提供要使用的自定义查询。

@Query(value = "SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments …",
       countQuery = "select count(v) from VisitEntity v where …")
List<VisitEntity> getVenueVisits(@Param("venueId") long venueId, …);

#2


1  

You have to specify countQuery param for @Query and now you can use Page or List as return value.

您必须为@Query指定countQuery参数,现在您可以使用Page或List作为返回值。

@Query(value = "SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ...",
       countQuery = "SELECT count(v) FROM VisitEntity v LEFT JOIN v.comments WHERE v.venue.id = :venueId and ..." )
public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
        Pageable pageable);

#3


1  

Alternatively in newest versions of Spring (supporting JPA 2.1 specification) you can use entity graph like this:

或者在最新版本的Spring(支持JPA 2.1规范)中,您可以使用如下的实体图:

@EntityGraph(attributePaths = "roles")
@Query("FROM User user")
Page<User> findAllWithRoles(Pageable pageable);

Of course named entity graphs work as well.

当然命名的实体图也可以工作。

#1


39  

The easiest way is to use the countQuery attribute of the the @Query annotation to provide a custom query to be used.

最简单的方法是使用@Query批注的countQuery属性来提供要使用的自定义查询。

@Query(value = "SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments …",
       countQuery = "select count(v) from VisitEntity v where …")
List<VisitEntity> getVenueVisits(@Param("venueId") long venueId, …);

#2


1  

You have to specify countQuery param for @Query and now you can use Page or List as return value.

您必须为@Query指定countQuery参数,现在您可以使用Page或List作为返回值。

@Query(value = "SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ...",
       countQuery = "SELECT count(v) FROM VisitEntity v LEFT JOIN v.comments WHERE v.venue.id = :venueId and ..." )
public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
        Pageable pageable);

#3


1  

Alternatively in newest versions of Spring (supporting JPA 2.1 specification) you can use entity graph like this:

或者在最新版本的Spring(支持JPA 2.1规范)中,您可以使用如下的实体图:

@EntityGraph(attributePaths = "roles")
@Query("FROM User user")
Page<User> findAllWithRoles(Pageable pageable);

Of course named entity graphs work as well.

当然命名的实体图也可以工作。