Hibernatetemplate only supports the. setmaxresults (INT) method.
Therefore, you need to use the org. springframework. Orm. hibernate3.hibernatecallback interface for spring + hibernate paging processing.
To operate the database flexibly. This interface has an unimplemented method object doinhibernate (session) for obtaining and using sessions for operations (automatic creation and destruction ).
BelowCodeRefer to the document on paging query using hibernatetemplate.
/***/ /**
*
*/
Package Springdao;
Import Hibernatedao. hibernatesessionfactory;
Import Java. SQL. sqlexception;
Import Java. util. List;
Import Org. Apache. commons. Logging. log;
Import Org. Apache. commons. Logging. logfactory;
Import Org. hibernate. hibernateexception;
Import Org. hibernate. Session;
Import Org. springframework. Orm. hibernate3.hibernatecallback;
Import Org. springframework. Orm. hibernate3.hibernatetemplate;
/***/ /**
* General Dao package
*@ AuthorKiant
*@ VersionSep 7, 2008
*/
Public Class Commomsdao {
Private Static Final Log = Logfactory. getlog (ecopusdao. Class );
// Get session
Private Static Hibernatetemplate = New Hibernatetemplate (hibernatesessionfactory. getsessionfactory ());
/***/ /***
* General paging method
* @ Param hql query statement
* @ Param subscript of the Offset start record
* @ Param lengh read records
* @ return List result set
*/
Public Static List getlistforpage ( Final String hql, Final Int Offset, Final Int Lengh) {
Log. debug ( " Finding listforpage " );
Try {
List list = Hibernatetemplate.exe cutefind ( New Hibernatecallback () {
Public Object doinhibernate (session)
Throws Hibernateexception, sqlexception {
List list2 = Session. createquery (hql)
. Setfirstresult (offset)
. Setmaxresults (lengh)
. List ();
Return List2;
} } );
Return List;
} Catch (Runtimeexception re) {
Log. Error ("Find listforpage failed", RE );
ThrowRe;
}
}
}
PS.
you can also use:
This. getsession ();
This. gethibernatetemplate (). getsessionfactory (). opensession ();
obtain sessions and perform createquery () and other operations respectively.
however, You need to manually close the session. Therefore, you need to configure opensessioninview, which is not recommended!