java 在 QueryDSL 中使用 CollectionExpression
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7107502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using CollectionExpression in QueryDSL
提问by Abhinav Sarkar
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>
.
在 QueryDSL 库中,com.mysema.query.types.expr.SimpleExpression<T>
该类有一个SimpleExpression.in(CollectionExpression<?, ? extends T>)
方法,该方法应该采用一个应该返回集合的表达式。但是我找不到创建 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:
我希望它expression
是这样的:
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>
.
但是第二个表达式是类型的com.mysema.query.types.expr.BooleanExpression
,我无法找到将其转换为com.mysema.query.types.CollectionExpression<?, ? extends T>
.
I looked in the QueryDSL API docsbut could not find anything relevant.
我查看了QueryDSL API 文档,但找不到任何相关内容。
回答by Timo Westk?mper
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.
您不能将 BooleanExpression 转换为 CollectionExpression,原因与不能将 java.lang.Boolean 转换为 java.util.Collection 的原因相同。它们不兼容。
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.
subQuery() 的实现是特定于 Querydsl 后端的。如果您使用连接,那么您会为每个匹配的事件 - 组织组合和子查询获得一行,您将获得具有满足给定约束的组织的独特事件。
Performance differences of join vs subquery are implementation specific.
连接与子查询的性能差异是特定于实现的。
Which Querydsl backend do you use? JPA, SQL or something else?
您使用哪个 Querydsl 后端?JPA、SQL 或其他什么?
回答by Sean Patrick Floyd
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
您必须使用子查询