QueryDsl – 集合表达式中的子查询

我正在使用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的更多JPAesqueforms的答案

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