17

I have a Person JPA entity, and my Person has multiple addresses (OneToMany relationship from Person to Address). I want to be able to do a query for all people that have a particle zipcode but I'm not sure after looking at the querydsl documentation how to properly handle the collection.

I can access the addresses but I'm not sure what to do with them:

QPerson qPerson = QPerson.person;
personDao.findAll(qPerson.addresses._SPECIFICADDRESS_.zip.eq('73130'));

How can I get the SPECIFICADDRESS I'm looking for?

codeLes
  • 3,021
  • 3
  • 29
  • 27

1 Answers1

30

Try this

QPerson qPerson = QPerson.person;
personDao.findAll(qPerson.addresses.any().zip.eq('73130'));
Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
  • Looks too simple... will try it as soon as I'm back at a terminal. Thanks! – codeLes Jun 02 '12 at 21:27
  • This is great. What's really getting me now is I have to get even more specific down a stack of collections with collections... This if really fun figuring out though. Thanks for the help. – codeLes Jun 03 '12 at 18:00
  • You can express deeper structures with explicit subqueries. any() creates those internally, and for simple structures it's a good alternative. – Timo Westkämper Jun 04 '12 at 10:17
  • 1
    Can you share an example of a subquery in above example where I need to check 2 params instead of just one, zip. For eg : where zip = '73130' and stdCode = '0028' ?? I tried with subquery but I am not able to execute it. – coretechie Apr 10 '17 at 11:02