views:

21

answers:

1

The following code causes an exception when the Job table has no rows.

public List<Job> getAll(int currentPage, int pageSize) {
    return this.sessionFactory.getCurrentSession()
        .createCriteria(Job.class).addOrder(Order.asc("id"))
        .setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
        .setFetchSize(pageSize).list();
}

I am using SQL Server and the JTDS driver.

The error i get is

java.sql.SQLException: ResultSet may only be accessed in a forward direction.
 net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:319)
 net.sourceforge.jtds.jdbc.JtdsResultSet.absolute(JtdsResultSet.java:716)
 org.apache.commons.dbcp.DelegatingResultSet.absolute(DelegatingResultSet.java:335)
 org.hibernate.loader.Loader.advance(Loader.java:1469)
 org.hibernate.loader.Loader.getResultSet(Loader.java:1783)
 org.hibernate.loader.Loader.doQuery(Loader.java:662)
 org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
 org.hibernate.loader.Loader.doList(Loader.java:2211)
 org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
 org.hibernate.loader.Loader.list(Loader.java:2090)
 org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
 org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
 org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
+1  A: 

The issue is associated with trying to page an empty table. drop these:
.setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
.setFetchSize(pageSize)
and you should be able to query the empty table without issue. If you want to page the data, run a regular query first, then page the data with your query after you know you have data to page.

ebt
It works fine when using MySQL. It seems to only fail with SQL Server.
Anthony