Some help requesting sleep mode

Can someone please help me fill out / clear this request. I try to get the number of rows in a table first, then I need to actually get a rowset. I am not sure how I can use the same instance of the criteria to fulfill both queries.

To get the counter, I have something like this:

Criteria criteria = session.createCriteria(MyTable.class); criteria.setProjection(Projections.rowCount()); Integer count = (Integer) criteria.uniqueResult(); int numRows = count.intValue(); 

And to extract the rows (I only need a subset for entire pages):

 Criteria criteria = session.createCriteria(MyTable.class); criteria.setFirstResult(offset); criteria.setMaxResults(limit); criteria.addOrder(Order.desc(orderBy.toString())); List<MyType> myType = criteria.list(); 

Do I need to Zero the projection first or something like that so that I can use the criteria to extract the rows (after doing the count)?

I would really like to help clear this so that I can efficiently execute both queries and eventually get the total number of rows and a list of results. Thanks!!

+4
source share
2 answers

You need to clear the projection of the reference and install a result transformer to extract MyTable objects. Try this for a second query:

criteria.setProjection (zero)

criteria.setResultTransformer (Criteria.ROOT_ENTITY);

+6
source

You ask about reusing the same criteria for calculation and forecasting, right? I could not find a way to do this, so I put the criteria and limitations in my own method and built it twice ... once for counting and once for data page. If there is a better way, I'm all ears ...

+1
source

Source: https://habr.com/ru/post/1300466/


All Articles