2010-06-07 126 views
1

這情況下,我有:
我有兩個實體一個一對多的關係映射這樣的(只給出相關部分):如何按HQL查詢中的列表進行排序?

 
    <class name="xyz.Survey"> 
     <list name="answers" inverse="true" lazy="true" fetch="select" 
cascade="all-delete-orphan"> 
      <key column="OPA_OP_ID" not-null="true" /> 
      <list-index column="OPA_SORT_ORDER" /> 
      <one-to-many class="xyz.Answer" /> 
     </list> 
    </class> 
    <class name="xyz.Answer"> 
     <many-to-one name="survey" class="xyz.Survey" fetch="select"> 
      <column name="OPA_OP_ID" not-null="true" /> 
     </many-to-one> 
there is no mapping for column OPA_SORT_ORDER, I let Hibernate take care of that 
    </class> 

我的問題是:我想查詢執行HQL單一調查的答案,我希望這些答案按OPA_SORT_ORDER排序。該查詢不起作用這樣的:

 
select new AnswerDTO(a.id, a.answerText, 0) 
from Survey p 
join p.answers a 
where p.id = ? 

在日誌中我可以看到,有沒有這個的HQL產生的ORDER BY在SQL查詢。我怎樣才能做到這一點?我是否需要嚮應答實體添加sortOrder屬性?

回答

4

不,你並不需要添加屬性,HQL有此內置功能index:使用標準它

select new AnswerDTO(a.id, a.answerText, 0) 
from Survey p 
join p.answers a 
where p.id = ? 
order by index(a) 
+0

我能做什麼? – RAS 2012-08-17 07:11:29

相關問題