QueryDsl - 集合表达式中的子查询 [英] QueryDsl - subquery in collection expression

查看:1602
本文介绍了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.

推荐答案

以下是更多JPAesque表格中jaiwo99答案的变体

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.nu‌​mber));

随意将其合并到原始答案中。

Feel free to merge this into the original answer.

这篇关于QueryDsl - 集合表达式中的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆