2010-03-04 122 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