I'm super rusty with databases and haven't got the chance to work too much with them in the past 6-8 years, so there are some things that are uncertain for me.
Let's assume I have a simple bi-directional one-to-many situation:
Parent
------
id
and
Child
-----
id
age
parent_id
and I want to perform a query which will return all the parents together with the matching children that follow some rules.
For example let's assume we have
Parent: [Child (age 5), Child (age 8), Child (age 10)]
Parent: [Child (age 8), Child (age 15)]
Parent: [Child (age 20)]
and I want to obtain the parents together with their children filtered for example by age (less than 10).
This situations screams a join with a where statement - something like:
SELECT Parent.id, Child.id, Child.age
FROM Parent LEFT JOIN Child ON Parent.id = Child.parent_id
WHERE Child.age < 10;
And this will work fine and return 3 entries - two rows for parent1 with the children that match the rule and one for parent two and its children.
However, my question is - can I group it somehow based on Parent Id?
Can I use LIMIT for limiting the number of Parents? (For example in this case limit 2 will return all results - because there are two parents that match the criteria)
Background context - I'm trying to implement this with Java, Hibernate and a Postgres db. I'm using OneToMany/ManyToOne relationships and CriteriaQuery in order to interogate the DB.