chaitu chaitu
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.h ibernate.S essionFact ory) in class org.springframework.orm.hi bernate.su pport.Hibe rnat eDaoSupport cannot be overridden by method setSessionFactory(net.sf.h ibernate.S essionFact ory) in class db.EmpObjectDaoImpl
import java.util.*;
import bus.Employee;
import org.springframework.orm.hi bernate.Hi bernateTem plat e;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.orm.hi bernate.su pport.Hibe rnat eDaoSupport;
import org.springframework.orm.hi bernate.Se ssionFacto ryUt ils;
import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
import net.sf.hibernate.SessionFa ctory;
import net.sf.hibernate.Hibernate Exception;
import net.sf.hibernate.Scrollabl eResults;
public class EmpObjectDaoImpl extends HibernateDaoSupport implements EmpObjectDao {
Employee emp = new Employee();
private SessionFactory sessionFactory;
public void setSessionFactory(SessionF actory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* getAllMyObjectVOs
*
* @return List
* @todo Implement this org.annotationmvc.dao.MyOb jectDao 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;
}
}
when i run this class i am getting
Error(25,17): final method setSessionFactory(net.sf.h
import java.util.*;
import bus.Employee;
import org.springframework.orm.hi
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.orm.hi
import org.springframework.orm.hi
import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
import net.sf.hibernate.SessionFa
import net.sf.hibernate.Hibernate
import net.sf.hibernate.Scrollabl
public class EmpObjectDaoImpl extends HibernateDaoSupport implements EmpObjectDao {
Employee emp = new Employee();
private SessionFactory sessionFactory;
public void setSessionFactory(SessionF
this.sessionFactory = sessionFactory;
}
/**
* getAllMyObjectVOs
*
* @return List
* @todo Implement this org.annotationmvc.dao.MyOb
*/
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Session sess = getSessionFactory().openSe ssion();
ASKER
what is PAGE_SIZE here???how will get PAGE_SIZE
you've set PAGE_SIZE to 10
int PAGE_SIZE=10;
int PAGE_SIZE=10;
ASKER
in the consol after 3 records its showing
java.lang.ArrayIndexOutOfB oundsExcep tion: 1
net.sf.hibernate.impl.Scro llableResu ltsImpl.ge t(Scrollab leResultsI mpl.java:1 27)
db.EmpObjectDaoImpl.getAll MyObjectVO s(EmpObjec tDaoImpl.j ava:85)
java.lang.ArrayIndexOutOfB
net.sf.hibernate.impl.Scro
db.EmpObjectDaoImpl.getAll
ASKER
i think PAGE_SIZE initialization is wrong
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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) );
seems more likely your result row contains less than 2 objects
> while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );
ASKER
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