Detachedcriteria paging projection cancel aggregation Conditions

Source: Internet
Author: User
@ Override
Public pagebean findpaginated (detachedcriteria, pagebean ){
// Todo, remember to modify and remove projection from detachedcriteria.
// Obtain the total number of records based on the Condition
Long Total = This. getrowcount (detachedcriteria );
List <t> datas = This. findbycriteria (detachedcriteria, pagebean. getoffset (), pagebean. getpagesize ());
Pagebean. setdatas (datas );
Pagebean. settotal (total );
Return pagebean;
}

// Use the specified search criteria to obtain the number of records meeting the criteria
Public long getrowcount (final detachedcriteria ){
Return gethibernatetemplate(cmd.exe cute (New hibernatecallback (){
@ Override
Public object doinhibernate (session)
Throws hibernateexception, sqlexception {
Criteria = detachedcriteria. getexecutablecriteria (session );
Criteriaimpl = (criteriaimpl) criteria;
Projection projection = criteriaimpl. getprojection ();
Logger. debug ("SQL:" + projections. rowcount ());
Long totalcount = (Java. Lang. Long) criteria. setprojection (projections. rowcount (). uniqueresult ();
Criteria. setprojection (projection );
If (projection = NULL ){
Criteria. setresulttransformer (criteriaspecification. root_entity );
}
Return totalcount;
}
});

}

Public list findbycriteria (detachedcriteria criteria, int firstresult,
Int maxresults ){
Return gethibernatetemplate (). findbycriteria (criteria, firstresult,
Maxresults );
}

This problem has plagued me for a long time. After the total number of rows is queried each time, the object information cannot be queried during the query, even if setprojectinos is null, however, the query result is of the List <object []> type when associated.

That is to say, there is still a problem with detachedcriteria. I cannot tell the specific reason, but this is also a solution. I hope it will be helpful to everyone,

In this way, detachedcriteria can be encapsulated to query the total number of rows under the paging condition,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.