QueryDsl - 集合表达式中的子查询
我正在使用 spring-data-jpa 和 querydsl (3.2.3)
我有一个场景,我正在根据用户文件管理器/输入创建一组谓词.所有这些都来自 BooleanExpression
.
I'm using spring-data-jpa and querydsl (3.2.3)
I have a scenario where I'm creating set of predicates based on user filer/input. All of these comes to BooleanExpression
.
我的简化模型如下所示:
My simplified model looks as following:
@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
}
现在,我正在努力解决这个问题:
Now, what I'm struggling with is this query:
SELECT * FROM Invoice WHERE invoice.supplier.number in (SELECT number from Company where active=true)
所以基本上我需要以 CollectionExpression
之类的格式进行子查询,该格式将获取所有公司编号并将其设置为 in() 表达式.
So basically I need to subquery in CollectionExpression
like format that will fetch all companies numbers and sets this into in() expression.
我的 spring-data 存储库实现了 CustomQueryDslJpaRepository
,后者又扩展了 JpaRepository
和 QueryDslPredicateExecutor
.
我希望这个问题的答案是直截了当的,但我对 querydsl 很陌生,到目前为止还没有找到解决方案.
My spring-data repositories implements CustomQueryDslJpaRepository
which in turn extends JpaRepository
and QueryDslPredicateExecutor
.
I hope the answer to this is straightforward, but I'm quite new to querydsl and didn't find the solutions so far.
推荐答案
这里是 jaiwo99 答案的变体,更 JPAesque 形式
Here is a variant of jaiwo99's answer in a more JPAesque form
BooleanExpression exp = invoice.supplier.number.in(new JPASubQuery()
.from(company)
.where(company.active.isTrue())
.list(company.number));
请随意将其合并到原始答案中.
Feel free to merge this into the original answer.
相关文章