@ 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,