簡體   English   中英

QueryDsl - 集合表達式中的子查詢

[英]QueryDsl - subquery in collection expression

我正在使用spring-data-jpa和querydsl(3.2.3)
我有一個場景,我正在創建基於用戶文件管理器/輸入的謂詞集。 所有這些都來自BooleanExpression

我的簡化模型如下:

@Entity
public class Invoice {
    @ManyToOne
    private Supplier supplier;
}

@Entity
public class Supplier {
    private String number;
}

@Entity
public class Company {
    private String number;
    private boolean active
}

現在,我正在努力解決的是這個問題:

SELECT * FROM Invoice WHERE invoice.supplier.number in (SELECT number from Company where active=true)

所以基本上我需要在CollectionExpression使用子查詢格式來獲取所有公司數字並將其設置為in()表達式。

我的spring-data存儲庫實現了CustomQueryDslJpaRepository ,后者又擴展了JpaRepositoryQueryDslPredicateExecutor
我希望答案是直截了當的,但我對querydsl很新,到目前為止還沒有找到解決方案。

以下是jaiwo99的更多JPAesque形式的答案

BooleanExpression exp = invoice.supplier.number.in(new JPASubQuery()
    .from(company)
    .where(company.active.isTrue())
    .list(company.nu‌​mber));

隨意將其合並到原始答案中。

試試這個:

QInvoice invoice = QInvoice.invoice;
QCompany company = QCompany.company;

List<Invoice> list = new HibernateQuery(sessionFactory.getCurrentSession())
      .from(invoice).where(
      new HibernateSubQuery().from(invoice, company).where(
              invoice.supplier.number.eq(company.number).and(
              company.active.eq(true))).exists()).list(invoice);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM