Question

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.

Was it helpful?

Solution

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?

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top