Using CollectionExpression in QueryDSL

In the QueryDSL library, the class com.mysema.query.types.expr.SimpleExpression<T> has a method SimpleExpression.in(CollectionExpression<?, ? extends T>) , which must accept an expression that should return a collection. But I can not find a way to create an object of type com.mysema.query.types.CollectionExpression<?, ? extends T> com.mysema.query.types.CollectionExpression<?, ? extends T> .

Your query expression looks like this:

 QEvent.event.organization.in(expression) 

where I want expression be something like:

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

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

I looked through QueryDSL API Docs , but couldn't find anything relevant.

+4
source share
2 answers

You cannot convert BooleanExpression to CollectionExpression for the same reasons why you cannot convert java.lang.Boolean to java.util.Collection. They are incompatible.

What would the following expression mean to you

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

Perhaps you are trying 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 easier

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

I assume that you tried to describe this expression using subqueries. Something like that

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

The subQuery () implementation is specific to Querydsl. If you use a connection, you get a row for each combination of the corresponding events - organizations, and with subqueries you get unique events that have organizations that satisfy the given restrictions.

The performance differences for the join vs subquery are implementation specific.

Which Querydsl backend are you using? Jpa, sql or something else?

+11
source

I donโ€™t think you can do it like this, it wonโ€™t make sense on the DB side, anyway.

You must use subquery

+1
source

All Articles