gpt4 book ai didi

jpa-2.0 - JPA条件查询Path.get左联接是否可能

转载 作者:行者123 更新时间:2023-12-04 03:53:45 25 4
gpt4 key购买 nike

我对JPA标准有疑问。

这是我的JPA标准查询:

 CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder();
CriteriaQuery<InventoryItemSumReport> query = criteriaBuilder.createQuery(InventoryItemSumReport.class);
Root<InventoryItemDetail> from = query.from(InventoryItemDetail.class);

Join<InventoryItemDetail, InventoryItem> joinItem = from.join(InventoryItemDetail_.inventoryItem);

Predicate where = criteriaBuilder.lessThanOrEqualTo(from.get(InventoryItemDetail_.effectiveDate), date);

query.multiselect(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
query.groupBy(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
query.where(where);

TypedQuery<InventoryItemSumReport> createQuery = getEm().createQuery(query);
List<InventoryItemSumReport> resultList = createQuery.getResultList();

这是JPA提供程序产生的结果查询:
    select
inventoryi1_.PRODUCT_ID as col_0_0_,
inventoryi1_.FACILITY_ID as col_1_0_,
inventoryi1_.CUSTOMER_ID as col_2_0_
from
INVENTORY_ITEM_DETAIL inventoryi0_
inner join
INVENTORY_ITEM inventoryi1_
on inventoryi0_.INVENTORY_ITEM_ID=inventoryi1_.ID
inner join
PRODUCT product2_
on inventoryi1_.PRODUCT_ID=product2_.ID
inner join
FACILITY facility3_
on inventoryi1_.FACILITY_ID=facility3_.ID
inner join
CUSTOMER customer4_
on inventoryi1_.CUSTOMER_ID=customer4_.ID
where
inventoryi0_.EFFECTIVE_DATE<= ?
group by
inventoryi1_.PRODUCT_ID ,
inventoryi1_.FACILITY_ID ,
inventoryi1_.CUSTOMER_ID

但是我想要以下查询:
    select
inventoryi1_.PRODUCT_ID as col_0_0_,
inventoryi1_.FACILITY_ID as col_1_0_,
inventoryi1_.CUSTOMER_ID as col_2_0_
from
INVENTORY_ITEM_DETAIL inventoryi0_
inner join
INVENTORY_ITEM inventoryi1_
on inventoryi0_.INVENTORY_ITEM_ID=inventoryi1_.ID
inner join
PRODUCT product2_
on inventoryi1_.PRODUCT_ID=product2_.ID
inner join
FACILITY facility3_
on inventoryi1_.FACILITY_ID=facility3_.ID
left join
CUSTOMER customer4_
on inventoryi1_.CUSTOMER_ID=customer4_.ID
where
inventoryi0_.EFFECTIVE_DATE<= ?
group by
inventoryi1_.PRODUCT_ID ,
inventoryi1_.FACILITY_ID ,
inventoryi1_.CUSTOMER_ID

使用 左键,加入CUSTOMER 也可以得到 Customers为null的结果。 CustomerProductFacility都是实体,而 InventoryItemSumReport是Value对象或DTO。
public class InventoryItemSumReport implements Serializable {

private static final long serialVersionUID = 1L;

private Product product;
private Facility facility;
private Customer customer;

public InventoryItemSumReport(Product product, Facility facility, Customer customer) {
super();
this.product = product;
this.facility = facility;
this.customer = customer;
}
}

最佳答案

我发现如下工作:

 CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder();
CriteriaQuery<InventoryItemSumReport> query = criteriaBuilder.createQuery(InventoryItemSumReport.class);
Root<InventoryItemDetail> from = query.from(InventoryItemDetail.class);

Join<InventoryItemDetail, InventoryItem> joinItem = from.join(InventoryItemDetail_.inventoryItem);

Predicate where = criteriaBuilder.lessThanOrEqualTo(from.get(InventoryItemDetail_.effectiveDate), date);

Join<InventoryItem, Customer> joinCustomer = joinItem.join(InventoryItem_.customer, JoinType.LEFT);
query.multiselect(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinItem.get(InventoryItem_.customer));
query.groupBy(joinItem.get(InventoryItem_.product),joinItem.get(InventoryItem_.facility),joinCustomer);
query.where(where);

TypedQuery<InventoryItemSumReport> createQuery = getEm().createQuery(query);
List<InventoryItemSumReport> resultList = createQuery.getResultList();

关于jpa-2.0 - JPA条件查询Path.get左联接是否可能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10145074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com