Question

I am having trouble formulating the following query. I have a Parent.class and Child.class. Each parent can have many children and each child can have many parents, so it is a bi-directional many-to-many association.

I need to get all parents that do not have specific child and sort them by some property, eg. parent.age.

The answer with most upvotes in this question looks like it could be adapted to fit my problem, but it is said in the comments that when using sub-queries it is not possible to sort by Parent property.

Ideally I would like to achieve it with Criteria, but HQL would be OK too..

Was it helpful?

Solution

select distinct p from Parent p, Child c 
where c.id = :childId
and c not member of p.children
order by p.age
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top