只是面对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会进行一些其他查询以获取类别。
您的存储库接口是否扩展了JpaRepository
或PagingAndSortingRepository
,例如这样(其中Long
是您实体的类型@Id
字段:
@Repository
interface ProductRepository extends JpaRepository<Product, Long> {
// your custom repo methods...
}