Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CollectionExpression in QueryDSL

Tags:

java

querydsl

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.

like image 789
Abhinav Sarkar Avatar asked Aug 18 '11 12:08

Abhinav Sarkar


1 Answers

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?

like image 100
Timo Westkämper Avatar answered Oct 11 '22 12:10

Timo Westkämper