Java JPA标准路径。获取联接的“ID”<;A、 B>;返回B的ID而不是A的ID,为什么?
我正在使用一个子查询,该子查询的目的是查找没有特定值的CustomerItems,它由Java JPA标准路径。获取联接的“ID”<;A、 B>;返回B的ID而不是A的ID,为什么?,java,jpa,eclipselink,criteria,criteria-api,Java,Jpa,Eclipselink,Criteria,Criteria Api,我正在使用一个子查询,该子查询的目的是查找没有特定值的CustomerItems,它由ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())返回。这个查询目前正在做我希望它做的事情,除了一件事;选择连接的错误ID 我希望表达式itemPropertiesJoin.get(ID)指向CustomerItem的ID的路径?我错过什么了吗?生成的查询应该是从CustomerItem中选择ID,如下所示:selectdistinct t
ecoLabelHelper.getEcoProperties(filterCriteria.getCustomer())
返回。这个查询目前正在做我希望它做的事情,除了一件事;选择连接的错误ID
我希望表达式itemPropertiesJoin.get(ID)
指向CustomerItem的ID的路径?我错过什么了吗?生成的查询应该是从CustomerItem中选择ID,如下所示:selectdistinct t10.ID
,但它会获取itemselectdistinct t11.ID
的ID
项目:
子查询:
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()));
干杯
我希望表达式itemPropertiesJoin.get(ID)指向CustomerItem的ID的路径
为什么会这样?如果您想要
CustomerItem
的id,请使用itemProperties.get(id)
我想。在本例中,联接的get(“id”)将引用联接的第一个实体CustomerItem。无论如何,现在我在select语句中将子查询的最后一行更改为itemProperties.get(ID)
,查询将在没有连接的情况下执行。自解释,因为它不再是谓词中的引用。如何使连接条件成为谓词的一部分?有什么想法吗?你说的“使连接条件成为谓词的一部分”是什么意思?你到底想达到什么目的?我也不知道为什么要使用.on()
。只需在子查询中添加一个.in()
谓词。我的想法是在on()
条件下使用连接,因为我认为它会执行得更好。相反,我只是更改了on()
方法中的条件,使其位于where()
中,现在它可以正常工作。谢谢你的帮助!
@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')