JPA Criteria Path.get("ID") of a Join<A, B> returns B 的 ID 而不是 A 的 ID,为什么?
JPA Criteria Path.get("ID") of a Join<A, B> returns ID of B instead ID of A, why?
我正在使用一个子查询,该子查询旨在查找 ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())
返回的没有某些特定值的 CustomerItems。该查询目前正在做我想要它做的事情,除了一件事;选择了错误的连接 ID。
我希望表达式 itemPropertiesJoin.<Integer>get(ID)
指向 CustomerItem 的 ID 路径?我错过了什么吗?生成的查询应该从 CustomerItem 中选择 ID,如下所示:SELECT DISTINCT t10.ID
,但它确实采用了项目的 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')
干杯,
I expect the expression itemPropertiesJoin.get(ID) to point to the path of the ID of CustomerItem?
为什么会这样?如果你想要CustomerItem
的id,使用itemProperties.get(id)
我正在使用一个子查询,该子查询旨在查找 ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())
返回的没有某些特定值的 CustomerItems。该查询目前正在做我想要它做的事情,除了一件事;选择了错误的连接 ID。
我希望表达式 itemPropertiesJoin.<Integer>get(ID)
指向 CustomerItem 的 ID 路径?我错过了什么吗?生成的查询应该从 CustomerItem 中选择 ID,如下所示:SELECT DISTINCT t10.ID
,但它确实采用了项目的 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')
干杯,
I expect the expression itemPropertiesJoin.get(ID) to point to the path of the ID of CustomerItem?
为什么会这样?如果你想要CustomerItem
的id,使用itemProperties.get(id)