@Batchsize 注释不适用于 OneToMany
@Batchsize annotation not working for OneToMany
我有以下 类 并且在注释 @BatchSize 注释时它不起作用,我收到 n+1 select 查询。
Class Shipment{
@OneToMany(fetch = FetchType.LAZY, mappedBy = order.shipment, cascade = CascadeType.ALL,
orphanRemoval = true)
@BatchSize(size=20)
Set<Orders> orders = new Hashset(); <---- Batch size annotation not working
}
Order.class
class Order{
@ToString.Exclude
@ManyToOne
@JoinColumn(name = "item_fk")
Item item;
@ToString.Exclude
@ManyToOne
@JoinColumn(name = "shipment_fk")
Shipment shipment; }
Item.class
class Item{
String id;
String name;
}
我收到 n+1 个查询的实现有什么错误?
尝试使用 List<Orders>
而不是 Set<Orders>
。
请注意 documentation:
However, although @BatchSize
is better than running into an N+1 query issue, most of the time, a DTO projection or a JOIN FETCH
is a much better alternative since it allows you to fetch all the required data with a single query.
您的 N+1 查询问题是由于您在 Order
中急切获取 Item
。在那里更改为 LAZY,您应该可以开始了。
我有以下 类 并且在注释 @BatchSize 注释时它不起作用,我收到 n+1 select 查询。
Class Shipment{
@OneToMany(fetch = FetchType.LAZY, mappedBy = order.shipment, cascade = CascadeType.ALL,
orphanRemoval = true)
@BatchSize(size=20)
Set<Orders> orders = new Hashset(); <---- Batch size annotation not working
}
Order.class
class Order{
@ToString.Exclude
@ManyToOne
@JoinColumn(name = "item_fk")
Item item;
@ToString.Exclude
@ManyToOne
@JoinColumn(name = "shipment_fk")
Shipment shipment; }
Item.class
class Item{
String id;
String name;
}
我收到 n+1 个查询的实现有什么错误?
尝试使用
List<Orders>
而不是Set<Orders>
。请注意 documentation:
However, although
@BatchSize
is better than running into an N+1 query issue, most of the time, a DTO projection or aJOIN FETCH
is a much better alternative since it allows you to fetch all the required data with a single query.
您的 N+1 查询问题是由于您在 Order
中急切获取 Item
。在那里更改为 LAZY,您应该可以开始了。