Integration of spring after hibernatetemplate implementation paging

Source: Internet
Author: User

Spring integration Hibernate use hibernatetemplate does not support paging, so you need to package a class for paging, the implementation of the following ... Extend the functionality of Hibernatetemplate by using spring's hibernatetemplate callback mechanism for paging

/*** operation with HQL statement *@paramHQL *@paramOffset *@paramlength *@returnList*/     PublicList Getlistforpage (FinalString HQL,Final intOffset,Final intlength) {List List= Gethibernatetemplate (). Executefind (NewHibernatecallback () { PublicObject Doinhibernate (Session session)throwshibernateexception, SQLException {query Query=session.createquery (HQL);          Query.setfirstresult (offset);          Query.setmaxresults (length); List List=query.list (); returnlist;        }        }); returnlist; }/*** operation with criterion *@paramArg *@paramcriterions *@paramOffset *@paramlength *@returnList*/    protectedList Getlistforpage (FinalClass ARG,FinalCriterion[] Criterions,Final intOffsetFinal intlength) {List List= Gethibernatetemplate (). Executefind (NewHibernatecallback () { PublicObject Doinhibernate (Session session)throwsHibernateexception, SQLException {criteria Criteria=Session.createcriteria (ARG); //looping through the addition of constraint conditions      for(inti = 0; i < criterions.length; i++) {Criteria.add (criterions[i]);          } criteria.setfirstresult (offset);          Criteria.setmaxresults (length); returncriteria.list ();        }        }); returnlist; }         //get the total number of collections (aggregate query)     PublicInteger Getsalechancelistcount () {FinalString hql = "SELECT count (*) from Salechanceinfo si"; Integer result=NULL; Result= (Integer) gethibernatetemplate (). Execute (NewHibernatecallback () { PublicObject doinhibernate (Session arg0)throwshibernateexception, SQLException {query Query=arg0.createquery (HQL); returnQuery.uniqueresult ();             }                 }); returnresult; }    

Integration of spring after hibernatetemplate implementation paging

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.