jpa criteria-api:加入子选择

Posted

技术标签:

【中文标题】jpa criteria-api:加入子选择【英文标题】:jpa criteria-api: join with subselect 【发布时间】:2015-02-15 00:59:10 【问题描述】:

此查询用于检索一对多关系中的最后一条记录(请参阅 SQL join: selecting the last records in a one-to-many relationship)

SELECT  p.*
FROM    customer c 
        INNER JOIN (
                      SELECT customer_id, MAX(date) MaxDate
                      FROM purchase
                      GROUP BY customer_id
                    ) MaxDates ON c.id = MaxDates.customer_id 
        INNER JOIN purchase p ON MaxDates.customer_id = p.customer_id
                    AND MaxDates.MaxDate = p.date;

我的问题: 如何使用带有 jpa 标准 api 的子选择构建此连接?可能吗?如果没有,可以用 jpql 吗?

到目前为止我的代码:

final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Purchase> query = cb.createQuery(Purchase.class);
final Root<CustomerEntity> root = query.from(Customer.class);

// here should come the join with the sub-select

final Path<Purchase> path = root.join(Customer_.purchases);
query.select(path);

final TypedQuery<Purchase> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();

【问题讨论】:

解决这个问题以供参考和练习会很有趣,尽管您可能在 3 年后不再需要它。请给出表的表结构好吗? 【参考方案1】:

使用 JPA2.0 无法实现这样的查询,但我们可以通过重构查询来解决它

SELECT  p.*
FROM    customer c 
        /* This part gets the maximum date of a customer purchase
           We will replace it with a subquery in the where
        INNER JOIN (
                      SELECT customer_id, MAX(date) MaxDate
                      FROM purchase
                      GROUP BY customer_id
                    ) MaxDates ON c.id = MaxDates.customer_id */
        /* This part crosses the maximum date of a customer with the purchase itself to obtain the information
        INNER JOIN purchase p ON MaxDates.customer_id = p.customer_id
                    AND MaxDates.MaxDate = p.date*/
-- We make the crossing with the purchase (there will be N tickets per customer, with N being the number of purchases)
INNER JOIN purchase p on p.customer_id = c.id
-- In the where clause we add a condition so that these N entries become that of the     maximum date
WHERE p.date = (
    SELECT MAX(p2.date)
    FROM purchase p2
    WHERE p2.customer_id = c.id)
;

criteria-api 的实现是这样的

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Purchase> query = cb.createQuery(Purchase.class);
Root<Customer> root = query.from(Customer.class);
Join<Customer,Purchase> join = root.join(root.get("purchases"),JoinType.INNER);

Subquery<Date> sqMaxdate = cq.subquery();
Root<Purchase> sqRoot = sqMaxDate.from(Purchase.class);
Join<Purchase,Consumer> sqJoin = sqRoot.join(sqRoot.get("customer"),JoinType.INNER)
sqMaxDate.select(cb.max(sqRoot.get("date")));
sqMaxDate.where(cb.equal(sqJoin.get("id"),root.get("id")));

query.where(cb.equal(join.get("date"),sqMaxDate.getSelection()));
query.select(join);

TypedQuery<Purchase> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();

【讨论】:

以上是关于jpa criteria-api:加入子选择的主要内容,如果未能解决你的问题,请参考以下文章

带有两个子查询/连接的 JPA 标准选择

JPA双向实体:在查询父实体时仅选择子实体的子集

如何在 JPA Criteriabuilder Select 语句中执行子查询?

使用 JPA 连接从父实体返回子实体

k8s 读书笔记 - CRI(容器运行时接口)详解

在 JPA 的 SQL 查询中的 FROM 语句中包含子查询