Using querydsl how can I check against a specific object from a set of objects that result from a One to Many relationship?

codeLes picture codeLes · Jun 1, 2012 · Viewed 7.7k times · Source

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?

Answer

Timo Westkämper picture Timo Westkämper · Jun 2, 2012

Try this

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