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.
Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.
Use the uniqueResult() API method of Criteria to get the single instance that matches the query. Use again getTransaction() API method of Session and commit() API method of Transaction to commit the Transaction .
JPQL JPQL provides a simple and straightforward way to get all entities from a table. Let's see what it might look like to retrieve all students from a table using JPQL: Our Hibernate session's createQuery () method receives a typed query string as the first argument and the entity's type as the second.
In the below hibernate example we are using the MYSQL database. you can use any database you just need to update the configuration to change the database connection. Yes, To read the data we must have to insert the data into the database, here is a separate example to insert data into hibernate.
To retrieve data from the database, you simply leverage the Hibernate session�s createQuery method, pass in some HQL, convert the guts of the returned Query object into an easy to use List, and then loop through the collection of your POJOs contained in the List. It�s just that easy! What does the hibernate config file look like?
Yes, To read the data we must have to insert the data into the database, here is a separate example to insert data into hibernate. Add dependency for Hibernate and MySQL connector in the project (pom.xml) Create Hibernate configuration file (hibernate.cfg.xml) Configure Session Factory and Session and start the transections
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.
Try as following to get all rows from table
@SuppressWarnings("unchecked")
public List<Service> Service getServiceData() {
return ServiceDaoImpl.getSession().createQuery("from Service").list();
}
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);
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<>();
}
}
public List<Service> getServiceAll(){
return ServiceDaoImpl.getSession().createCriteria(Service.class).list();
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With