面对Spring Data JPA 2.2.0的奇怪行为。
Product和Category是具有一对多关系的两个非常简单的实体。请注意,就我而言,某些产品可能没有类别。
我进行查询
@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[]>
而不是Page<Product>
。如果我在返回类型中将Page
更改为List
,一切都会好的。为什么这样工作?可以更改这种行为吗?
我使用select p,c
通过一个查询用产品和类别中的所有数据填充结果产品。如果没有c
,则Hibernate不会执行其他一些查询来获取类别。