Last active
August 29, 2015 13:57
-
-
Save malalanayake/9532896 to your computer and use it in GitHub Desktop.
Pagination with Hibernate Criteria in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Admin> getAllAdminsWithPagination(int page, int recordePerPage) { | |
session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
Transaction tr = null; | |
try { | |
tr = session.beginTransaction(); | |
Criteria cr = session.createCriteria(Admin.class); | |
cr.setFirstResult((page - 1) * recordePerPage); | |
cr.setMaxResults(recordePerPage); | |
List<Admin> adminAll = cr.list(); | |
tr.commit(); | |
if (adminAll.isEmpty()) { | |
if (log.isDebugEnabled()) { | |
log.debug("Admin users are not exist"); | |
} | |
return null; | |
} else { | |
if (log.isDebugEnabled()) { | |
log.debug("Found " + adminAll.size() + " Admin users"); | |
} | |
return adminAll; | |
} | |
} catch (RuntimeException ex) { | |
log.error(ex); | |
if (tr != null) { | |
tr.rollback(); // roll back the transaction due to runtime error | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment