简体   繁体   中英

Hibernate Criteria Pagination

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM