如何解决JPA Criteria Path.get("ID") of a Join<A, B> 返回 B 的 ID 而不是 A 的 ID,为什么?
我正在使用一个子查询,旨在查找没有特定值的 CustomerItems,该值由 ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())
返回。查询目前正在做我想要它做的事情,除了一件事;选择了错误的连接 ID。
我希望表达式 itemPropertiesJoin.<Integer>get(ID)
指向 CustomerItem 的 ID 的路径?我错过了什么吗?结果查询应该是从 CustomerItem 中选择 ID,如下所示:SELECT DISTINCT t10.ID
,但它确实需要项目 SELECT DISTINCT t11.ID
的 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 的路径?
为什么会这样?如果您想要 CustomerItem
的 id,请使用 itemProperties.get(id)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。