2013-07-04 37 views
0

我在使用createCriteria()構建的Grails中查詢。我如何強制對結果執行排序,然後將偏移量應用於最大約束。在查詢偏移量和最大值之前強制排序

代碼:

def history = Termin.createCriteria().list([max:10, offset:offset]) { 
    and{ 
//some query constraints 
} 
    order('id', 'desc') 
} 

這可能會做同樣的:

def history = Termin.createCriteria().list([max:10, offset:offset,order:'desc',sort:'id']) { 
      and{ 
     //some query constraints 
     } 
} 

該數據庫PostgreSQL的7.5,內置的查詢看起來是這樣的:

select 
    something 
from 
    somewhere 
left outer join 
    something 
     on something 
left outer join 
    something 
     on something 
left outer join 
    something 
     on something 
where 
    (
     something 
    ) 
order by 
    this_.th_id desc limit ? offset ? 

回答

0

的排序是在查詢中使用offset和max時,Hibernate已經正確處理了。

如果啓用輸出,您將看到生成的查詢。例如,在Oracle中,他們有一個竅門,因爲它不包含查詢的這個函數。生成的查詢的

例子:

select * from 
(select a.*, ROWNUM rnum from 
    (<your_query_goes_here, with order by>) a 
    where ROWNUM <= :MAX_ROW_TO_FETCH) 
where rnum >= :MIN_ROW_TO_FETCH; 
+0

在我的情況下令僅10個結果不是整個結果... – Jacob

+0

然後,你需要提供更多的信息。女巫數據庫你使用?生成的查詢是什麼? –

+0

更新的問題.... – Jacob