English 中文(简体)
Using outer joins in HQL queries
原标题:

I m trying to build a query using HQL and OUTER JOINs and just can t get 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"d like to get a list of all Parents and the amount of the Parents children. Suppose I have one Parent with two children and one Parent with no children at all. I d expect an output like

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

using plain SQL it isn t a problem at all, I ll get this output 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 doesn t seem to be possible using HQL as one needs a path from Parent to Child when using a OUTER JOIN, which I obviously don t have (and also don t want to add).

Any ideas how to get the query working using HQL or is it really - I couldn t believe it - a missing hibernate feature?

最佳回答

Switching the query should help

SELECT p.id as parent, count(c.id) as children from children c right outer join c.parent p group by p.id;
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签