views:

823

answers:

2

I have an NHibernate Dao..lets call it MyClassDao for want of a better name.

I am writing the following code.

MyClassDao myDao = new MyClassDao();

var values = myDao.GetByCriteria(Restrictions.Eq("Status", someStatusValue));

I am using this in a Unit Test to pull back values from the database. However, it is taking over 30 seconds to run the test which is too long in my opinion...so what I'd like to do is limit the result set being pulled back...say to about 5 values.

in sql I would do something like the following to to achieve something like this

set rowcount 5
select * from whatever_table
set rowcount 0

Is there a way...without using the NHibernate Query language to restrict the size of a result set?

+8  A: 

Use ICriteria.SetMaxResults()

Jon Skeet
@Jon is there a way to get page of results, say range 40-49 of matching criteria? or should i be asking this as a separate question?
dove
Hey Jon, thanks for your reply..I believe it is the correct answer, but I'm kind of struggling to get it to work. I've looked ICriteria up in NHibernate and it seems I need a session...but I'm not quite sure where to get that...I've tried a few things but it hasn't worked.
mezoid
@dove: Based on Hibernate, I'd expect a SetFirstResult method on ICriteria. I haven't seen any documentation for it though.
Jon Skeet
@mezoid: Well, there must be a session *somewhere*. Chances are it's in your dao class at the moment. We can't see the source of GetByCriteria though - that's probably where it's creating the ICriteria.
Jon Skeet
@mezoid: Just checked, and there is indeed an ICriteria.SetFirstResult.
Jon Skeet
@mnezoid i think Jon's gone and answered it and my sub question too. thanks Jon.
dove
+2  A: 

You can use the SetMaxResults method on IQuery (if you use HQL) or ICriteria (if you use criteria API).

Darin Dimitrov