如何使用jpa标准api:
进行此类查询public static Specification<OrderEntity> x(String orderId) {
return (root, query, cb) -> {
Root<OrderItemEntity> itemRoot = query.from(OrderItemEntity.class);
query.multiselect(root.get(OrderEntity_.orderNo), cb.sum(itemRoot.get(OrderItemEntity_.quantity)));
return cb.equal(root.get(OrderEntity_.orderNo), orderId);
};
}
我尝试过这样的事情:
select order.orderNo from order join order_item on ...
OrderEntity是第一个表,OrderItemEntity是第二个表。我想从OrderEntity表中检索一些列,并从OrderItemEntity表中的列上检索一些列。上面的规范导致查询如下所示:
{{1}}
因此,OrderItemEntity表已加入,但聚合列未包含在查询中。
答案 0 :(得分:0)
尝试创建连接并从中访问列:
Join<OrderEntity, OrderItemEntity> itemJoin = root.join(OrderEntity_.your_join_column);
query.multiselect(root.get(OrderEntity_.orderNo), cb.sum(itemJoin.get(OrderItemEntity_.quantity)));
return cb.equal(root.get(OrderEntity_.orderNo), orderId);