Java Spring数据分页

Java Spring数据分页,java,hibernate,pagination,spring-data-jpa,Java,Hibernate,Pagination,Spring Data Jpa,只需面对Spring数据JPA2.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)

只需面对Spring数据JPA2.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);
@Query(“选择p,c”+
“来自产品p”+
“在p.Category.id=c.id上左连接获取类别c”+
“其中(:categoryId=-1L或c.id=:categoryId)和”+
“(:priceFrom为空或p.price>=:priceFrom)和”+

(:priceTo为空或p.price您的存储库界面是否扩展了
JpaRepository
分页和排序存储库
,例如,如下所示(其中
Long
是您的实体的类型
@Id
字段:

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

    // your custom repo methods...
}
@存储库
接口ProductRepository扩展了JpaRepository{
//您的自定义回购方法。。。
}
试试这个

@Query("from Product p left join fetch p.category c " +
       "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);
@Query(“从产品p左连接获取p.c类”+
“其中(:categoryId=-1L或c.id=:categoryId)和”+
“(:priceFrom为空或p.price>=:priceFrom)和”+

(:priceTo为null或p.price为了计算正确的页面信息,Spring JPA还需要最大行数。(用于计算页码)。为了获得此值,除了原始查询定义外,您还需要提供一个
countQuery

@Query(value = "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)",
       countQuery = "select count(p.id)" +
        "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)",
@Query(value=“选择p,c”+
“来自产品p”+
“在p.Category.id=c.id上左连接获取类别c”+
“其中(:categoryId=-1L或c.id=:categoryId)和”+
“(:priceFrom为空或p.price>=:priceFrom)和”+
“(:priceTo为空或p.price=:priceFrom)和”+

(:priceTo为null或p.price如果使用现有的Spring数据回购方法,例如
Page findAll(),是否也会发生这种情况
?可能还会发布相关的实体类…可能有助于复制…请看一看:据我所知,要使用字段的延迟加载,它必须具有
optional=false
参数。但是
Category
是可选的。您可以尝试添加
EmptyCategory
值,而不是null。然后尝试使用
加入fetch
而不是
left join fetch
此查询通过单独的select获取每个类别。我正在寻找一种方法,通过一个带有连接的查询强制Hibernate这样做。