1. 程式人生 > >hibernate 中 hql distinct注意事項

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 的列中。