views:

56

answers:

1

Hello everyone!

I'm making a query that sorts and returns X rows based on row_number() I'm using NHibernate with MSSQL and im trying to get paging working using CreateSQLQuery and i have this query:

select s.*   

from(

select distinct release.[stop], survey.SurveyId, survey.Created, survey.CopyOfId, survey.DesignTemplateId, survey.UserId, survey.Template, [Row] = Row_Number() over (order by survey.[SurveyId])

from    Survey               as survey
inner join  Release              as release  on release.SurveyId   = survey.SurveyId

group by    survey.SurveyId
,           survey.Created
,           survey.CopyOfId
,           survey.DesignTemplateId
,           survey.UserId
,           survey.Template
,   release.[stop]

) as s

where s.[Row] >= 0 and s.[Row] <= 20
order by s.[stop]

does anyone know how to get this working using HQL or ICriteria (even better) instead of plain SQL? The reason for this is that I want a query that is compatible with both SQLite and MS SQL Server 2005, so that i can use .SetMaxResult() og .SetFirstResult()

Thanks in advance!

+1  A: 

Try to avoid using plain SQL in nHibernate.

On Criteria object, use SetFirstResult() and SetMaxResult() for your paging.

Pages of 10 records ? First page is criteria.SetFirstResult(0).SetMaxResult(10) and third page is criteria.SetFirstResult(20).SetMaxResult(10)

Always use the correct dialect. For exemple SQL Server 2008 has more paging features than SQL Server 2005.

Pierre 303
You can also use a MultiCriteria to make a second query with a total count on the same roundtrip, to get the total number of pages.
jishi
Thanks for the answer, but i wonder how this query could look like in HQL or using ICriteria
The same, you can call those methods on the query object too.
Pierre 303
Hi, Pierre and thank you for your comment. It's not the same, which is why I'm asking this question. I'm wondering what the exact HQL query or series of ICriteria method chains I would need to build the same underlying SQL query as the one above.I know that if I build the above SQL with HQL or ICriteria, I get the `SetFirstResult()` and `SetMaxResult()` methods, but I'm unable to translate the SQL into HQL or ICriteria which is why I'm asking this in the first place.
What version of nHibernate are you using ? I just tested and the two methods are there http://www.surcombe.com/nhibernate-1.2/api/html/AllMembers_T_NHibernate_IQuery.htm
Pierre 303