[英]How toget a list of POJOs with distinct HQL in hibernate?
我試圖在休眠狀態下使用'distinct'HQL子句執行查詢並獲取列表。 但是,每當我使用'ditinct'時,我都會得到一個Java對象列表,而不是不使用'distinct'返回的POJO。 有沒有一種好的方法來獲取波霍音樂而不是物體?
該示例返回預期的POJO列表:
class myPojos () { ... (has some properties/fields) propOne, propTwo, propThree ...}
在DAO內部
Session s = HibernateUtil.currentSession();
List<myPojos> myPojoList = s
.createQuery( "select from " getPojoClass().getName())
.list();
但是,添加“ distinct”子句
List<myPojos> myPojoList = s
.createQuery( "select distcinct mpl.propOne, mpl.propTwo, mpl.propThree from " + getPojoClass().getName() + "mpl" )
.list();
...而是返回對象列表。 因此,我松開了myPojos類的所有getter和setter和松散的類型安全。
有沒有辦法做到這一點,並獲得一個列表以獲取retatin getter / setteres和type-saftey?
您可以使用以下方法: select p from Pojo p where p.id in (select distinct id from Pojo)
。 您正在子查詢中執行非重復操作,僅將已經唯一選擇的ID的選擇交給主查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.