Reputation: 143
public Service getServiceData(){
return (Service)ServiceDaoImpl.getSession().get(Service.class, new Integer(1));
}
The get method is getting one. I want to get all data for the jsp page.
Upvotes: 14
Views: 60223
Reputation: 1
By using the native query
Session session = sessionFactory.openSession();
try {
List<Book> bookList = session.createNativeQuery("Select * from book", Book.class).getResultList();
return bookList;
}catch (Exception e){
return null;
}finally {
session.close();
}
Upvotes: 0
Reputation: 2076
Since List<Entity> list = session.createCriteria(Entity.class).list();
was deprecated,
You can use CriteriaBuilder()
.
try ( Session session = sessionFactory.openSession()) {
// Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();
// Create CriteriaQuery
CriteriaQuery<Entity> criteria = builder.createQuery(Entity.class);
// Specify criteria root
criteria.from(Entity.class);
// Execute query
List<Entity> entityList = session.createQuery(criteria).getResultList();
for (Entity e : entityList) {
// load the data
}
}
Click here for the github example.
Upvotes: 5
Reputation: 5220
With the Hibernate 5 the createCriteria
is deprecated use something like this
private static <T> List<T> loadAllData(Class<T> type, Session session) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<T> criteria = builder.createQuery(type);
criteria.from(type);
List<T> data = session.createQuery(criteria).getResultList();
return data;
}
usage
Session session = HibernateUtils.getSession();
List<User> users = loadAllData(User.class, session);
Upvotes: 19
Reputation: 151
Try below piece of code and replace Entity with your Entity class
@SuppressWarnings("unchecked")
public List<Entity> getAlldata(){
try
{
return sessionFactory.getCurrentSession().createCriteria(Entity.class).list();
} catch (Exception e) {
return new ArrayList<>();
}
}
Upvotes: 10
Reputation: 67
Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
/* Selecting all objects(records) start_______________________ */
Query qry = session.createQuery("from Product p");
List l =qry.list();
System.out.println("Total Number Of Records : "+l.size());
Iterator it = l.iterator();
while(it.hasNext())
{
Object o = (Object)it.next();
Product p = (Product)o;
System.out.println("Product id : "+p.getProductId());
System.out.println("Product Name : "+p.getProName());
System.out.println("Product Price : "+p.getPrice());
System.out.println("----------------------");
}
session.close();
factory.close();
Upvotes: 3
Reputation: 71
public List<Service> getServiceAll(){
return ServiceDaoImpl.getSession().createCriteria(Service.class).list();
}
Upvotes: 4
Reputation: 713
Try as following to get all rows from table
@SuppressWarnings("unchecked")
public List<Service> Service getServiceData() {
return ServiceDaoImpl.getSession().createQuery("from Service").list();
}
Upvotes: 8