usage of SetPath.any() and multiple predicates.

5,733 views
Skip to first unread message

Thomas Bruyelle

unread,
Jun 15, 2012, 11:44:10 AM6/15/12
to quer...@googlegroups.com
Consider a JPA entity (QUser.user) which has a collection attribute of another entity (QUser.user.pets), if you want to request this entity on a field of this other entity, you can write your predicate like that with Querydsl :

QUser.user.pets.any().type.eq( 'dog' )

The where clause generated in JPQL :

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1)


Now if I want to base my request on multiple fields of the other entity, how can I do ? If I write something like that :

QUser.user.pets.any().type.eq( 'dog' ).and( QUser.user.pets.any().color.eq( 'red' ) )

The JPQL generated is not correct :

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1) AND exists (select pet from Pet pet where pet member of user.pets and pet.color = ?2)

The JPQL I would like to have :

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1 and pet.color = ?2)


How can I do with querydsl ?

Thomas Bruyelle

unread,
Jun 15, 2012, 11:47:03 AM6/15/12
to quer...@googlegroups.com
> The JPQL generated is not correct 

Would be The JPQL generated is not what I expected :)

Timo Westkämper

unread,
Jun 16, 2012, 11:07:37 AM6/16/12
to Querydsl on behalf of Thomas Bruyelle
Hi.

I guess for this case you would need to write it as a subquery

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1 and pet.color = ?2)

becomes

subQuery.from(pet).where(pet.in(user.pets), pet.type.eq(...), pet.color.eq(...)).exists()

On Fri, Jun 15, 2012 at 6:47 PM, Querydsl on behalf of Thomas Bruyelle <quer...@googlegroups.com> wrote:
> The JPQL generated is not correct 

Would be The JPQL generated is not what I expected :)



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



Timo Westkämper

unread,
Jun 16, 2012, 11:08:46 AM6/16/12
to Querydsl on behalf of Thomas Bruyelle

Thomas Bruyelle

unread,
Jun 16, 2012, 1:39:58 PM6/16/12
to quer...@googlegroups.com
Awesome !! 
I tried the subqueries but without the exists() at the end, and I didn't manage to do what I want. Now with the usage of exists() it works like a charm !

Thanks Timo, once again you help me a lot
Have a nice day

Best regards


Le samedi 16 juin 2012 17:08:46 UTC+2, Timo Westkämper a écrit :
On Sat, Jun 16, 2012 at 6:07 PM, Timo Westkämper <> wrote:
Hi.

I guess for this case you would need to write it as a subquery

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1 and pet.color = ?2)

becomes

subQuery.from(pet).where(pet.in(user.pets), pet.type.eq(...), pet.color.eq(...)).exists()

On Fri, Jun 15, 2012 at 6:47 PM, Querydsl on behalf of Thomas Bruyelle <> wrote:
> The JPQL generated is not correct 

Would be The JPQL generated is not what I expected :)



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



Maciej Wojterski

unread,
Mar 30, 2016, 10:28:52 AM3/30/16
to Querydsl
Hi Timo,

I'm wondering if after those couple of years, maybe there is a better solution now? The problem I have with suggested approach is that I couldn't find any subquery factory or generic implementation, which would allow me to separate from actual query implementation and since it is supposed to be sub-query, this information should not be necessary. Currently I have a class which provides predicates but should not be aware of executor (possibly many different) which will run them.

Best Regards,
MWt

W dniu sobota, 16 czerwca 2012 17:07:37 UTC+2 użytkownik Timo Westkämper napisał:
Hi.

I guess for this case you would need to write it as a subquery

exists (select pet from Pet pet where pet member of user.pets and pet.type = ?1 and pet.color = ?2)

becomes

subQuery.from(pet).where(pet.in(user.pets), pet.type.eq(...), pet.color.eq(...)).exists()

timowest

unread,
Mar 30, 2016, 1:33:40 PM3/30/16
to Querydsl
Maciej, do you use Querydsl 3 or 4?

Maciej Wojterski

unread,
Mar 30, 2016, 2:54:09 PM3/30/16
to Querydsl
Timo, I'm using Querydsl v. 4.0.2 from com.querydsl group.

timowest

unread,
Mar 31, 2016, 11:54:06 AM3/31/16
to Querydsl
Subqueries in Querydsl 4 are now detached queries, you can create them for JPA via the static methods in JPAExpressions, e.g. JPAExpressions.select/selectFrom/selectOne

Maciej Wojterski

unread,
Mar 31, 2016, 12:07:35 PM3/31/16
to Querydsl
I know, I can as well just use new JPAQuery(), but the point is, the code is just creating a predicate and it basically need only depend on querydsl-core. It'd be great if I could not say it's a JPA subquery, since I can use created predicate with many different executors like Collections or MongoDB. So I was just wondering if there is a way to create some generic SubQuery (not tied to any particular executor).
Reply all
Reply to author
Forward
0 new messages