Using external joins in HQL queries

I am trying to build a query using HQL OUTER JOINand just can't make it work. Consider the following mapping

<class name="Parent">
    <id name="id" type="integer">
      <generator class="native" />
    </id> 
</class>

<class name="Child">
    <id name="id" type="integer">
      <generator class="native" />
    </id> 
    <many-to-one name="parent"/>
</class>

Now I would like to get a list of all parents and the number of parents' children. Suppose I have one parent with two children and one parent who has no children. I would expect a result like

+-------------------+
| parent | children |
+--------+----------+
|  1     | 2        |
|  2     | 0        |
+--------+----------+

using plain SQL is not a problem, I will get this conclusion by doing something like

SELECT p.id as parent, count(c.id) as children from parents p LEFT OUTER JOIN children c on c.parent_id = p.id group by p.id;

However, it seems impossible to use HQL, because> requires a path from parent to child, which I obviously do not have (and also do not want to add).

, HQL - - ?

+3
1

SELECT p.id as parent, count(c.id) as children from children c right outer join c.parent p group by p.id;
+3

Source: https://habr.com/ru/post/1724631/


All Articles