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

2024-08-23Java开发问题
59

本文介绍了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,后者又扩展了 JpaRepositoryQueryDslPredicateExecutor.
我希望这个问题的答案是直截了当的,但我对 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.

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13