Hibernate request. How to choose those parents who have ALL children matching the value?

I am trying to write a Hibernate (JPA) query to select those parents where ALL of their children match the property.

Let me make an example ...

I want to choose those fathers who have ALL of their children blonde. If only one black-haired father is not selected.

What will the request look like? Thank you in advance!

+5
source share
3 answers

Perhaps something like this will work:

From Father f where not exists (select c from f.children c where not c.hair = "BLONDE"); 

Just an idea ...

+4
source

Try this using ALL see http://openjpa.apache.org/builds/1.1.0/docs/jpa_langref.html#jpa_langref_all_any :

 select p from parent where 'blonde'=all(parent.children.haircolor) 
+1
source

This should also work, and it looks a little cleaner.

 SELECT p from Parent p join p.children c where c.haircolor = 'blonde'; 
+1
source

All Articles