Spring数据分页

问题描述 投票:0回答:1

只是面对Spring Data JPA 2.2.0的奇怪行为。

产品和类别是具有一对多关系的两个非常简单的实体。请注意,就我而言,某些产品可能没有类别。

我进行此查询

@Query("select p, c" +
        "from Product p " +
        "left join fetch Category c on p.category.id = c.id " +
        "where (:categoryId = -1L or c.id = :categoryId) and " +
        "(:priceFrom is null or p.price >= :priceFrom) and " +
        "(:priceTo is null or p.price <= :priceTo)")
Page<Product> filterProducts(@Param("categoryId") Long categoryId,
                             @Param("priceFrom") BigDecimal priceFrom,
                             @Param("priceTo") BigDecimal priceTo,
                             Pageable pageable);

但是方法调用返回Page<Object[]>而不是List<Product>。如果我在返回类型中将Page更改为List,一切都会好起来的。为什么这样工作?是否可以更改此行为?

我使用select p, c通过一个查询用产品和类别中的所有数据填充结果产品。如果没有c,Hibernate会进行一些其他查询以获取类别。

java hibernate pagination spring-data-jpa
1个回答
0
投票

您的存储库接口是否扩展了JpaRepositoryPagingAndSortingRepository,例如这样(其中Long是您实体的类型@Id字段:

@Repository
interface ProductRepository extends JpaRepository<Product, Long> {

    // your custom repo methods...
}
© www.soinside.com 2019 - 2024. All rights reserved.