jpa-2.0 - jpa 标准-api : join with subselect

标签 jpa-2.0 criteria-api

此查询用于检索一对多关系中的最后一条记录(请参阅
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();

最佳答案

使用 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)
;
使用标准 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-2.0 - jpa 标准-api : join with subselect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509322/

相关文章:

spring - Tomcat 7 Spring 应用程序 IntelliJ IDEA 10.5 OutOfMemoryError : PermGen space

hibernate - JPQL 右连接

java - 使用 NumberLong 在 mongodb 中创建查询时出现问题

java - JPA 2 + Criteria API - 定义子查询

java - 多个事务不是应该与 em.getTransaction() 一致吗?

java - 如何持久化实体,如何从没有 "main class"的实体创建表?

java - 多选 NPE 中的 JPA/Hibernate 函数

sql - sql minus的jpa 2.0标准api表达式

java - 您可以使用标准 API 控制 Hibernate 中的获取行为吗?

java - 对于子查询器 - 什么是好的? HQL 或标准 -DetachedCriteria