2009-12-03 61 views
3

我試圖使用HQL和OUTER JOIN來構建查詢,但無法讓它工作。考慮下面的映射在HQL查詢中使用外連接

<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> 

現在我的「D希望得到所有家長的名單和父母子女的數量。假設我有一個家長帶着兩個孩子和父母一方無子女的。我倒是希望像使用普通的SQL它不是在所有問題

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

的輸出,我會得到這個輸出做這樣的事情

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; 

但是似乎可能不使用HQL a需要路徑從父母到孩子使用OUTER JOIN時,我顯然沒有(也不想添加)。

任何想法如何使用HQL查詢工作,或者它真的 - 我無法相信 - 缺少休眠功能?

回答

3

切換查詢應該幫助

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