在 QueryDSL 中使用 CollectionExpression

发布于 2024-11-30 03:35:34 字数 773 浏览 2 评论 0原文

在 QueryDSL 库中,com.mysema.query.types.expr.SimpleExpression 类有一个 SimpleExpression.in(CollectionExpression)方法应该采用一个应该返回集合的表达式。但我找不到创建 com.mysema.query.types.CollectionExpression

我的查询表达式如下所示:

QEvent.event.organization.in(expression)

我希望 表达式 类似于:

QOrganization.organization.country.in("India", "USA")

但第二个表达式的类型为 com.mysema.query.types.expr.BooleanExpression 并且我无法找到将其转换为 com.mysema.query.types.CollectionExpression

我查看了 QueryDSL API 文档,但找不到任何相关内容。

In the QueryDSL library, the com.mysema.query.types.expr.SimpleExpression<T> class has a SimpleExpression.in(CollectionExpression<?, ? extends T>) method which is supposed to take an expression which is supposed to return a collection. But I cannot find a way to create an object of type com.mysema.query.types.CollectionExpression<?, ? extends T>.

My query expression looks like this:

QEvent.event.organization.in(expression)

where i want the expression to be something like:

QOrganization.organization.country.in("India", "USA")

But the second expression is of type com.mysema.query.types.expr.BooleanExpression and I am unable to find a way to convert it to com.mysema.query.types.CollectionExpression<?, ? extends T>.

I looked in the QueryDSL API docs but could not find anything relevant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2024-12-07 03:35:34

您无法将 BooleanExpression 转换为 CollectionExpression,其原因与您无法将 java.lang.Boolean 转换为 java.util.Collection 的原因相同。他们不兼容。

下面的表达对你来说意味着什么

QEvent.event.organization.in( 
    QOrganization.organization.country.in("India", "USA"))

你可能会尝试表达这样的东西吗?

QEvent event = QEvent.event;
QOrganization organization = QOrganization.organization;
query.from(event)
    .innerJoin(event.organization, organization)
    .where(organization.country.in("India", "USA"))
    .list(event);

或者更简单,

QEvent event = QEvent.event;
query.from(event)
    .where(event.organization.country.in("India", "USA"))
    .list(event);

我想您试图描述的是使用子查询的表达式。像这样的

query.from(event)
    .where(event.organization.in( subQuery().from(organization)
        .where(organization.country.in("India", "USA")))
    .list(event);

subQuery() 的实现是 Querydsl 后端特定的。如果您使用联接,那么您将获得每个匹配事件-组织组合的一行,并且通过子查询,您将获得组织满足给定约束的唯一事件。

连接与子查询的性能差异是特定于实现的。

您使用哪个 Querydsl 后端? JPA、SQL 还是其他?

You can't convert a BooleanExpression into CollectionExpression, for the same reasons why you can't convert a java.lang.Boolean into a java.util.Collection. They aren't compatible.

What would the following expression mean to you

QEvent.event.organization.in( 
    QOrganization.organization.country.in("India", "USA"))

Do you maybe try to express something like this?

QEvent event = QEvent.event;
QOrganization organization = QOrganization.organization;
query.from(event)
    .innerJoin(event.organization, organization)
    .where(organization.country.in("India", "USA"))
    .list(event);

Or simpler

QEvent event = QEvent.event;
query.from(event)
    .where(event.organization.country.in("India", "USA"))
    .list(event);

I guess what you tried to describe was an Expression using subqueries. Something like this

query.from(event)
    .where(event.organization.in( subQuery().from(organization)
        .where(organization.country.in("India", "USA")))
    .list(event);

The implementation of subQuery() is Querydsl backend specific. If you use a join then you get a row for each matching event - organization combination and with subqueries you get unique events which have organizations meeting the given constraints.

Performance differences of join vs subquery are implementation specific.

Which Querydsl backend do you use? JPA, SQL or something else?

笑忘罢 2024-12-07 03:35:34

我不认为你可以这样做,无论如何,这在数据库方面没有意义。

您必须使用子查询

I don't think you can do it like that, it wouldn't make sense on the DB side, anyway.

You have to use a SubQuery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文