2010-03-04 81 views
0

我有兩個對象:「母親」和「孩子」。 母親有很多孩子, 我怎樣才能從DB獲得只有2個孩子(或更少),年輕和年長的母親。 感謝HQL Min Max,如何

編輯:

的母親,我想有很多孩子,但我只希望年輕和年長。

類似的東西:

從母親米

左加入m.ChildÇ 其中(MAX(c.age)或分鐘(c.age))

回答

1

我發現這一點,它作品:

from M as m  
left join m.C as c 
where m.Id = :idM 
and 
(c.Age = (select min(c.Age) from C c where c.M.Id = :idM) 
or 
c.Age = (select max(c.Age) from C c where c.M.Id = :idM)) 
order by c.Age 
1
FROM Mother m WHERE count(m.Children) <= 2