Just face strange behavior of Spring Data JPA 2.2.0.
Product and Category are two very simple entities with one-to-many relations. Notice that in my case some products could have no category.
I make this query
@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);
But method call returns Page<Object[]>
instead of Page<Product>
. If I change Page
to List
in return type all going to be fine. Why it works this way? Is it possible to change this behavior?
I use select p, c
to fill resulting products with all data from product and category by one query. Without c
Hibernate doing some additional queries to get Categories.