0

What I need is add "pagination" in a List of a Class. Ej:

Class A
  List B

so I need limit the list of B to certain value

What I'm doing is (Using Criteria, and it's all dynamic):

ICriteria criteria = Session.createCriteria();

ICriteria criteriaParcial = criteria.CreateCriteria("B");

criteriaParcial.SetFirstResult(0).SetMaxResults(10);

criteria.list();

This is an example, the realy code is all dynamic and iterative.

This mechanisme works find filtering values, but not for pagination.

Any Idea? Tanks

Updated!!!

This Approach doesn't work becouse this method execution criteria.CreateCriteria("B").setMAxResults(10) Modifies to root criteria not subCriteria as I thought.

So my cuestion now is. It's possible to restrict or paginate, a list inside a class?

Something like this: I have a class Person with a list of address, so I want to load a Person, but only the two first objects inside the list of address.

aivaldi
  • 67
  • 2
  • 12
  • What exactly do you mean with "works find filtering values, but not for pagination" ? – Miroslav Popovic Jun 28 '12 at 21:36
  • I mean that instead of set the values of SetFirstResult, etc. I set a restriction like this `.add(Restrictions.eq("B", new B("somthing")))` it works and does the filtering, exactly the same code. – aivaldi Jun 29 '12 at 00:35
  • If you use eager fetching, you should try disabling it, as it may produce weird results with paging. Also, when I know there might be problems with eager fetching, I tend to use criteriaParcial.SetResultTransformer(NHibernate.Transform.Transformers.DistinctRootEntity).List() instead of simple List() – jbl Jun 29 '12 at 09:38
  • I've discovered my problem. I'm trying to pagiate over a list inside my Class, but 'criteriaParcial.SetMaxResults(10)' modifies to the root criteria, not to the list itself. So this approach doesn't Work – aivaldi Jun 29 '12 at 15:06

1 Answers1

0

It's not possible AFAIK. But the aproach is wrong either way. It's better to load the root entity data, and in another query load the sub lists, that way you can paginate filter and sort as you want.

List properties were not made for being displayed nice, they are just to contain a list of objects and that what they do. That's why creating another query that returns only those items is the best approach.

You can use the Future<> queries to improve the DB performance.

Diego Jancic
  • 7,280
  • 7
  • 52
  • 80