Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all data in the table with hibernate?

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.

like image 200
user1833860 Avatar asked Mar 27 '17 04:03

user1833860


People also ask

Which of the following method is used to retrieve records from table in Hibernate?

Query Object Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects.

How can I get single record in Hibernate?

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 .

How do I get all entities from a table in hibernate?

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.

Which database is used in hibernate?

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.

How do I retrieve data from 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?

How to insert data into hibernate from MySQL database?

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


6 Answers

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.

like image 198
Dil. Avatar answered Oct 05 '22 09:10

Dil.


Try as following to get all rows from table

@SuppressWarnings("unchecked")
    public List<Service> Service getServiceData() {

         return ServiceDaoImpl.getSession().createQuery("from Service").list(); 
    }
like image 24
Neha Shettar Avatar answered Oct 05 '22 11:10

Neha Shettar


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);
like image 35
Babu Avatar answered Oct 05 '22 09:10

Babu


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<>();
    }
}
like image 41
Radhika Avatar answered Oct 05 '22 10:10

Radhika


public List<Service> getServiceAll(){
     return ServiceDaoImpl.getSession().createCriteria(Service.class).list();
}
like image 27
richard ordoñez Avatar answered Oct 05 '22 10:10

richard ordoñez


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();
like image 30
Bhagvat Avatar answered Oct 05 '22 10:10

Bhagvat