Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

paging problem while doing with spring+Hibernate

i am trying to implement paging using Spring+Hibernate;what is PAGE_SIZE here?

when i run this class i am getting

Error(25,17): final method setSessionFactory(net.sf.hibernate.SessionFactory) in class org.springframework.orm.hibernate.support.Hibernat eDaoSupport cannot be overridden by method setSessionFactory(net.sf.hibernate.SessionFactory) in class db.EmpObjectDaoImpl



import java.util.*;
import bus.Employee;
import org.springframework.orm.hibernate.HibernateTemplat e;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.orm.hibernate.support.Hibernat eDaoSupport;
import org.springframework.orm.hibernate.SessionFactoryUt ils;
import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.ScrollableResults;

public class EmpObjectDaoImpl extends HibernateDaoSupport implements EmpObjectDao {

Employee emp = new Employee();
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}


/**
* getAllMyObjectVOs
*
* @return List
* @todo Implement this org.annotationmvc.dao.MyObjectDao method
*/
public List getAllMyObjectVOs() {

int PAGE_SIZE=10;
ArrayList pageOfCats =null;
ScrollableResults cats =null;


try {

Session sess= sessionFactory.openSession();
Query q = sess.createQuery("select cat.name, cat from DomesticCat cat " +
"order by cat.name");
cats = q.scroll();


if ( cats.first() ) {

// find the first name on each page of an alphabetical list of cats by name
ArrayList firstNamesOfPages = new ArrayList();
do {
String name = cats.getString(0);
firstNamesOfPages.add(name);
}
while ( cats.scroll(PAGE_SIZE) );

// Now get the first page of cats
pageOfCats = new ArrayList();
cats.beforeFirst();
int i=0;
while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );

}
cats.close();


}catch(HibernateException e)
{
e.printStackTrace();
System.out.println("catch block of getAllMyObjectVOs................");
}
return pageOfCats;
}







}
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chaitu chaitu

ASKER

if i remove that line how will i get session object;i am getting this exception

Error(62,15): variable sessionFactory not found in class db.EmpObjectDaoImpl

i am new to Hibernate also Is it correct get session;

Session sess= sessionFactory.openSession();
Session sess = getSessionFactory().openSession();
what is PAGE_SIZE here???how will get PAGE_SIZE
you've set PAGE_SIZE to 10

int PAGE_SIZE=10;

in the consol after 3 records its showing

java.lang.ArrayIndexOutOfBoundsException: 1
      net.sf.hibernate.impl.ScrollableResultsImpl.get(ScrollableResultsImpl.java:127)
      db.EmpObjectDaoImpl.getAllMyObjectVOs(EmpObjectDaoImpl.java:85)
i think PAGE_SIZE initialization is wrong
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what makes you ythink that?
seems more likely your result row contains less than 2 objects

> while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );