**
* 获取Session对象
*/
public Session getSession(){
EntityManager em=this.getJpaTemplate().getEntityManagerFactory().createEntityManager();
return (Session) em.getDelegate();
}
/**
* 创建DetachedCriteria对象
*/
@SuppressWarnings("hiding")
public
return DetachedCriteria.forClass(entityClass);
}
/**
* 分页查询函数,使用DetachedCriteria.
*
* @param startIndex 起始页,
* @param pageSize 每页显示条数
* @return PaginationSupport对象
*/
public PaginationSupport findPageBy(final DetachedCriteria detachedCriteria, final int pageSize, final int startIndex) {
JpaCallback jc=new JpaCallback() {
public Object doInJpa(EntityManager em) throws HibernateException {
Session session=(Session)em.getDelegate();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(null);
List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
return ps;
}
};
return (PaginationSupport)getJpaTemplate().execute(jc, true);
}
public class PaginationSupport {
public final static int PAGESIZE = 30;
private int pageSize = PAGESIZE;
private List items;
private int totalCount;
private int[] indexes = new int[0];
private int startIndex = 0;
public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}
public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}
public int[] getIndexes() {
return indexes;
}
public void setIndexes(int[] indexes) {
this.indexes = indexes;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}
public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}
public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2931)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
代码:
public class PagingTest {
EntityManagerFactory emf = null;
private javax.persistence.EntityManager em;
/**
* @param args
*/
public static void main(String[] args) {
PagingTest pTest= new PagingTest();
pTest.testPaging();
}
public void testPaging(){
emf = EntityManagerHelper.getEmf();
em= emf.createEntityManager();
Query rowCountQuery = em.createQuery("SELECT COUNT(c) FROM Customer c"); // NOI18N
int size =((Long) rowCountQuery.getSingleResult()).intValue();
System.out.println(size);
final Query getRowsQuery = em.createQuery("select c from Customer c");
System.out.println("numer of requests to the database " + counter++);
Query query = getRowsQuery.setMaxResults(20).setFirstResult(1000000);//这里如果设置为setFirstResult(100000); 就可以运行。
//add the cache
List
System.out.println("=====query done " );
for(Customer cust:resultList){
System.out.println("cust:"cust.getCustomerId()","+cust.getCustomerName());
}
}
private javax.persistence.EntityManager entityManager1;
private javax.persistence.Query getRowsQuery;
private javax.persistence.Query rowCountQuery;
private List
getRowsQuery = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT c FROM Customer c"); // NOI18N
rowCountQuery = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT COUNT(c) FROM Customer c"); // NOI18N
List
return toReturn;
}
private int counter=0;
}