jpa критериев-api: объединить с подзапросом

Этот запрос используется для получения последних записей в отношении «один ко многим» (см. Соединение SQL: выбор последних записей в отношении" один ко многим ")

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();

person user871611    schedule 16.12.2014    source источник
comment
Было бы интересно решить эту проблему для справки и упражнений, хотя, вероятно, через ›3 года она вам больше не понадобится. Приведите, пожалуйста, структуру таблиц?   -  person Karl Richter    schedule 19.04.2018


Ответы (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)
;

реализация с критериями-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();
person JLazar0    schedule 27.08.2020