简体   繁体   English

自定义JPA CriteriaQuery的子句

[英]Customize JPA CriteriaQuery's on-clause

Is there a way to further restrict a join by adding some expressions? 有没有一种方法可以通过添加一些表达式来进一步限制联接? With plain sql i write: 用普通的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

I haven't found a way to implement this with JPA 2.0 CriteriaQuery. 我还没有找到使用JPA 2.0 CriteriaQuery实施此方法的方法。

Update: As requested my (simplified) code so far (without the extra restriction): 更新:根据要求,到目前为止,我的代码(简化)(没有额外的限制):

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

您应该只能够添加条件谓词,而不必担心它们是否属于ON子句或WHERE子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM