java - JPA Criteria Path.get("ID") of a Join返回 B 的 ID 而不是 A 的 ID,为什么?
问题描述
我正在使用一个子查询,该子查询旨在查找没有某些特定值的 CustomerItems,该值由ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())
. 该查询目前正在做我想做的事,除了一件事;选择了错误的连接 ID。
我希望表达式itemPropertiesJoin.<Integer>get(ID)
指向 CustomerItem 的 ID 的路径?我错过了什么吗?生成的查询应该从 CustomerItem 中选择 ID,如下所示:SELECT DISTINCT t10.ID
,但它确实需要 item 的 ID SELECT DISTINCT t11.ID
。
物品:
@Entity
@Table(name = "mp_item", indexes = { @Index(columnList = "itemno, supplier_id"), @Index(columnList = "itemGroupIdSupplier, itemGroupSupplier"),
public class Item extends BaseShopEntity implements ItemIntrospection {
@ElementCollection
@CollectionTable(name = "mp_item_properties", joinColumns = @JoinColumn(name = "mp_item_id"))
@BatchFetch(value = BatchFetchType.JOIN)
private Set<String> itemProperties;
客户项目:
@Entity
@Table(name = "mp_customer_item", indexes = { @Index(columnList = "item_id, customer_id", unique = true) })
public class CustomerItem extends BaseShopEntity {
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
private Item item;
子查询:
Subquery<Integer> subquery = criteriaQuery.subquery(Integer.class);
Root<CustomerItem> itemProperties = subquery.from(CustomerItem.class);
Join<CustomerItem, Item> itemPropertiesJoin = (Join<CustomerItem, Item>) (Object) itemProperties.fetch(ITEM);
itemPropertiesJoin.on(itemPropertiesJoin.<String>get(ITEM_PROPERTIES).in(ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())));
subquery.select(itemPropertiesJoin.<Integer>get(ID));
subquery.distinct(true);
Predicate itemIsNotEco = criteriaBuilder.not(criteriaBuilder.in(root.<Integer>get(ID)).value(subquery.select(itemProperties.get(ID))));
conditions.add(criteriaBuilder.and(itemIsNotEco, customerItemIsEco.not()));
生成的查询:
NOT (
t1.ID IN (
SELECT
DISTINCT t10.ID
FROM
[mptest].[mptest].[mp_customer_item] t11,
[mptest].[mptest].[mp_item] t10,
[mptest].[mptest].[mp_item_properties] t12
WHERE
(
(t12.mp_item_id = t10.ID)
AND (
(t10.ID = t11.ITEM_ID)
AND (
t12.ITEMPROPERTIES IN (
'Z01',
'Z02',
'Z03',
'Z04',
'Z11',
'Z12',
'Z15',
'Z17',
'Z20',
'Z22',
'Z24',
'Z38',
'Z40',
'Z48',
'Z49',
'Z50',
'Z56',
'Z58',
'Z60',
'Z61',
'Z62',
'Z63',
'Z64',
'Z65',
'Z68'
)
)
)
)
)
)
AND NOT (
(t2.ECO = 'true')
干杯,
解决方案
我希望表达式 itemPropertiesJoin.get(ID) 指向 CustomerItem ID 的路径?
为什么会这样?如果你想要 的 id CustomerItem
,请使用itemProperties.get(id)
推荐阅读
- typescript - 如何在 React / Typescript 中使用强类型模型?
- java - Java自定义线程池未终止
- c++ - 单例类抛出 C2653 错误:不是类或命名空间名称
- scala - Spark读取空目录
- r - 使用 H2O R 模型进行预测时使用 optim 包时出错
- next.js - 我使用 next.js Head 元素错了吗?
- reactjs - npm start 后反应项目不自动加载
- javascript - dom更改后如何使用react hooks强制更新
- python-3.x - 从引用的 int 列表创建一个 int 列表
- javascript - 如何配置 ZingChart 的宽度和高度?