Spring-Data FETCH JOIN with Paging is not working

前端 未结 5 1724
野性不改
野性不改 2020-11-30 05:37

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

相关标签:
5条回答
  • 2020-11-30 06:03

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

    @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, …);
    
    0 讨论(0)
  • 2020-11-30 06:04

    Try countProjection

    @Query(value="SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ..." ,
    countProjection = "v.id")
    public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
        Pageable pageable);
    
    0 讨论(0)
  • 2020-11-30 06:07

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

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

    Of course named entity graphs work as well.

    0 讨论(0)
  • 2020-11-30 06:13

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

    @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);
    
    0 讨论(0)
  • 2020-11-30 06:14

    If you want completely control your query build by Specification with join fetch you can check CriteriaQuery return type and change join fetch logic according to query type like this:

    public class ContactSpecification implements Specification<Contact> {
        @Override
        public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            if(query.getResultType() == Long.class) {
                root.join(Contact_.company);
            } else {
                root.fetch(Contact_.company);
            }
            return cb.equal(root.get(Contact_.company).get(Company_.name), "Company 123");
        }
    }
    

    I was not able to find this info in documentation, but from SimpleJpaRepository.getCountQuery() method you can see query for count request first build for Long return type, and later fetch for expected class is running.

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    
    Root<S> root = applySpecificationToCriteria(spec, domainClass, query);
    

    It can be not reliable since it is an implementation details which can be changed, but it works.

    0 讨论(0)
提交回复
热议问题