Reusable QueryDSL JPA query with IN clause parameter

2,714 views
Skip to first unread message

Lawrence Fanizza

unread,
May 6, 2015, 1:28:02 PM5/6/15
to quer...@googlegroups.com
I know how to use a parameter in QueryDSL queries for single values like the following:

QFilmEntity fe = QFilmEntity.filmEntity;
Param<String> param = new Param<>(String.class,"rating");
JPAQuery query = new JPAQuery().from(fe).where(fe.rating.eq(param)).distinct();

I can set the parameter like this:

query.set(param,"G");


I now want to use a parameter for a collection of values like the following:

QFilmEntity fe = QFilmEntity.filmEntity;
JPAQuery query = new JPAQuery().from(fe).where(fe.rating.in([Some Collection Parameter]));

It looks like 'in' will accept a CollectionExpression but so far I have not figured out how to construct one so it is interpreted as a parameter.

Does QueryDSL support this feature? Any help on how to achieve this would be appreciated.

Michael Tontchev

unread,
May 6, 2015, 1:58:47 PM5/6/15
to quer...@googlegroups.com
I am under the impression that CollectionExpression cannot be used to refer to normal Java collections you'd have in your own code. CollectionExpression currently can represent either 1) the results of a subquery or 2) the path to a field in an Entity that is a collection, list, or set. As such, if you want to check for collection membership, I'd recommend using a BooleanExpression that compares the variable of interest to the possible values individually. My guess is that it would compile down to that anyway.

But I'm fairly new to QueryDSL, and I'm guessing others know more.

Lawrence Fanizza

unread,
May 6, 2015, 2:38:47 PM5/6/15
to quer...@googlegroups.com
Hi Michael,
thanks for the reply. I am new to QueryDSL as well. I am assuming there is a way to create a reusable query that uses a parameter capable of accepting multiple values. I store the queries with named parameters and then when I need to use them I clone and set the parameter values.
It would be great to treat the single value parameters and multiple value parameters in a similar way. Seems like it would be possible. Before I go looking for other solutions I just want to check if this feature already exists.


On Wednesday, May 6, 2015 at 1:28:02 PM UTC-4, Lawrence Fanizza wrote:

timowest

unread,
May 6, 2015, 3:30:57 PM5/6/15
to quer...@googlegroups.com

Timo Westkämper

unread,
May 6, 2015, 3:39:50 PM5/6/15
to Querydsl on behalf of timowest
Sorry, I missed the parameter usage of yours. I will try to find a solution.

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lawrence Fanizza

unread,
May 6, 2015, 3:40:30 PM5/6/15
to quer...@googlegroups.com

Thanks. I'm most likely just missing something obvious here. 
   So if I wanted to create a named parameter that represented a collection of values what would I do?
   
To reuse the example from before I'd want to do:
 QFilmEntity fe = QFilmEntity.filmEntity;
JPAQuery query = new JPAQuery().from(fe).where(fe.rating.in([Some Collection Parameter]));

What value can I pass to the IN operator that would indicate that this is a named parameter rather than a list of static values?
I would want to be able to set the value using query.set(param,listOfvalues);



Lawrence Fanizza

unread,
May 12, 2015, 9:13:45 AM5/12/15
to quer...@googlegroups.com
Hi Timo,
any luck looking for a solution? I am using predicates with parameters in my application and it would be really great if I could support collection parameters for In and Not In. I see that the com.mysema.query.types.expr.Param class implements com.mysema.query.types.Expression.  I don't see a parameter class that implements CollectionExpression. Any plans to add this? If you point me in the right direction I can try adding it myself.
Thanks


On Wednesday, May 6, 2015 at 1:28:02 PM UTC-4, Lawrence Fanizza wrote:

timowest

unread,
May 12, 2015, 11:45:28 AM5/12/15
to quer...@googlegroups.com
Could you create an issue for it? In the meantime you can create a custom expression for it that implements the needed interfaces.

Lawrence Fanizza

unread,
May 28, 2015, 7:53:12 AM5/28/15
to quer...@googlegroups.com
I added the issue. Sorry for the delay.


On Wednesday, May 6, 2015 at 1:28:02 PM UTC-4, Lawrence Fanizza wrote:
Reply all
Reply to author
Forward
0 new messages