自定义JPA CriteriaQuery的on-clause

时间:2013-06-05 15:25:23

标签: java jpa jpa-2.0 criteria-api on-clause

有没有办法通过添加一些表达式来进一步限制连接?用简单的sql编写:

SELECT c.*, COUNT(i.id) invoice_count
FROM customers c
LEFT JOIN invoices i ON i.customer_id = c.id
    AND i.creation_time >= '2012-01-01' -- <= extra restriction
    AND i.creation_time < '2013-01-01' -- <= extra restriction
GROUP BY c.id

我还没有找到一种方法来实现JPA 2.0 CriteriaQuery。

更新:按要求提供我的(简化)代码(没有额外限制):

CriteriaQuery<CustomerAndInvoiceCount> criteriaQuery = criteriaBuilder.createQuery(CustomerAndInvoiceCount.class);

Root<Customer> customer = criteriaQuery.from(Customer.class);
ListJoin<Customer, Invoice> invoices = customer.join(Customer_.invoices, JoinType.LEFT);

criteriaQuery.select(criteriaBuilder.construct(
        CustomerAndInvoiceCount.class,
        customer,
        criteriaBuilder.count(invoices)));
criteriaQuery.groupBy(customer);

1 个答案:

答案 0 :(得分:0)

您应该只能添加条件谓词,而不必担心它们是ON子句还是WHERE子句的一部分。