[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1110
  • Last Modified:

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;
}







}
0
chaitu chaitu
Asked:
chaitu chaitu
  • 4
  • 4
2 Solutions
 
objectsCommented:
private SessionFactory sessionFactory;

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


get rid of that, it is already provided by base class
0
 
chaitu chaituAuthor Commented:
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();
0
 
objectsCommented:
Session sess = getSessionFactory().openSession();
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
chaitu chaituAuthor Commented:
what is PAGE_SIZE here???how will get PAGE_SIZE
0
 
objectsCommented:
you've set PAGE_SIZE to 10

int PAGE_SIZE=10;

0
 
chaitu chaituAuthor Commented:
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)
0
 
chaitu chaituAuthor Commented:
i think PAGE_SIZE initialization is wrong
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
objectsCommented:
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) );

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now