有一个简单的DTO对象,如下所示,
@BatchSize(size=100)
public class ProductDto {
@BatchSize(size=100)
private Product product;
private UUID storeId;
private String storeName;
public ProductDto(Product product, UUID storeId, String storeName) {
super();
this.product = product;
this.storeId = storeId;
this.storeName = storeName;
}
// setters and getters ...
}
拥有Spring Data Repository,如下所示,
public interface ProductRepository extends CrudRepository<Product, java.util.UUID>
{
@BatchSize(size=100)
@Query("SELECT new com.app1.dto.ProductDto(c, r1.storeName, r1.id) "
+ "FROM Product c left outer join c.storeRef r1")
public Page<ProductDto> findProductList_withDTO(Pageable pageable);
}
当代码运行时,它会执行一个SQL来加载产品ID和StoreName以及StoreId
SELECT product0_.id AS col_0_0_,store1_.store_name AS col_1_0_,
store1_.id AS col_2_0_ FROM Product product0_
LEFT OUTER JOIN Store store1_ LIMIT 10
但问题是下一行,
对于数据库中存在的每个产品行,它执行SQL查询,而不是使用IN批量加载1个SQL中的所有匹配产品。
对于它执行的每个产品,
SELECT product0_.id AS id1_20_0_, product0_.prd_name AS prd_nam15_20_0_ FROM Product product0_ WHERE product0_.id = ?
SELECT product0_.id AS id1_20_0_, product0_.prd_name AS prd_nam15_20_0_ FROM Product product0_ WHERE product0_.id = ?
问题,
如何告知hibernate在单个SQL查询中加载所有产品行?我试过在所有地方添加@BatchSize(size = 100)。它还在执行多个查询来加载产品数据。
任何提示/解决方案都将受到赞赏。
由于 标记