hibernate 中 hql distinct注意事项

hql中 正常使用distinct没有任何问题,但是distinct和order by一起使用 就会有问题:
例如:User对象有 id、name、age三个属性
hql  :select distinct name,id from User order by age
这条hql 是有问题的,hibernate是无法执行,会报错。

正确写法:select distinct name,id,age from User order by age

意思就是说:当distinct 和order by一起使用,order by中的属性 必须被包含在 select 的列中。

hibernate 中 hql distinct注意事项