org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.loader.MultipleBagFetchException: can can同时获取多个包: [Order.items, OrderItem.options];
以上是我在加入三个表时遇到的异常,如下所示。
OrderItemOption.java
@Entity
public class OrderItemOption {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_option_id")
private Long id;
@Column(name = "item_id", nullable = false)
private Long itemId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "item_id",
referencedColumnName = "item_id",
insertable = false,
updatable = false
)
private OrderItem orderItem;
}
订单项.java
@Entity
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id")
private Long id;
@Column(name = "order_id", nullable = false)
private Long orderId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "order_id",
referencedColumnName = "order_id",
insertable = false,
updatable = false,
nullable = false
)
private Order order;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "orderItem")
@OrderBy("item_option_id ASC")
private List<OrderItemOption> options;
}
订购.java
@Entity
public class Order {
@Id
@Column(name = "order_id", nullable = false)
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order")
@OrderBy("item_id ASC")
private List<OrderItem> items;
}
这是我的QueryDSL代码,可以一次加入它们。
final QOrder order = QOrder.order;
final QOrderItem item = QOrderItem.orderItem;
final QOrderItemOption option = QOrderItemOption.orderItemOption;
from(order)
.leftJoin(order.items, item).fetchJoin()
.leftJoin(item.options, option).fetchJoin()
.where(
order.id.eq(orderId)
.and(item.id.in(itemIds))
.and(option.id.in(optionIds))
)
.fetchOne())
我试图做的是获取包含过滤关系的Order对象,以便我可以通过订单对象访问过滤的子级。关系的类型应该是列表,而不是集合。
例如,order.getItems().get(0).getOptions.get(0)
我怎样才能实现这个目标?
皈依舞
繁星点点滴滴
相关分类