有没有办法通过添加一些表达式来进一步限制连接?用简单的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);
答案 0 :(得分:0)
您应该只能添加条件谓词,而不必担心它们是ON子句还是WHERE子句的一部分。