How to Retrieve Objects using Hibernate

Retrieve Objects using Hibernate using session.get

In the previous tutorial series, we have seen how to save the object and we have also seen some useful annotations in Hibernate. In this tutorial you will learn at Retrieve Objects using Hibernate.

Till now, we know following things.

  • How to set up the environment for using hibernate?
  • How to Configure Hibernate and Create Model Object to Store user data?
  • How to use various Hibernate Annotations?

In this tutorial, we will learn how to retrieve stored values from the database using Hibernate. Every database has four basic operations known as CRUD- Creat Read Update Delete. In the previous tutorial, we have seen that Creat part is handled by Hibernate itself and we have to make use of session objects to Update or Save the data.

Letts try to understand the concept of Retrieve Objects using Hibernate with the help of simple Example.

Example of Retrieve Objects Using Hibernate

We will again use our previous example here.

Download Saving Objects with Hibernate Example Project

Edit the HibernateMain.java and close the session using this line of code session.close(); here session is the object that we have created using session class present in the package org.hibernate.Session.

Open a new session  to retrieve an object that we have previously saved. this could be done as session = factory.openSession();

To reuse the previously created object of the UserDetails Class in the Hibernate main class  we should have to set it to null using this line of code objectUser=null;

To began transaction with the database we will need a session object again, we will make use of previously created session object by just initializing it again using this line of code session.beginTransaction();

In the session object, we have a get function which can be called to retrieve data. This function has 2 parameters, first defines the Class of the object that we want to retrieve and second will define which object do we have to retrieve, each object is stored with a unique id called primary key. So we have to mention the primary key of the record that we want to retrieve. We can do all of this by using get function like this session.get(UserDetails.class, 1);

Note:If you have ever used SQL you know that we tell the table name to retrieve data from, while in hibernate we deal with our database in term of objects, that is why we have passes UserDetails.class in the get function. it will tell the hibernate about the object type.

The value that “session.get(class,recordId)” method returns will be the object of the class that we have passed so we have to capture the result in that object session.get(UserDetails.class, 1); will then become objectUser = (UserDetails) session.get(UserDetails.class, 1);

Then use this objects getter functions to retrieve objects values and display them in console using System.out.print().

Finally set hbm2ddl.auto to “create” in hibernat.cfg.xml, so that it will recreate the whole schema again.

The final form of HibernateMain.java can be seen below with the source code of retrieving user object named “objectUser”.

Updated HibernateMain.java

Test Run of Retrieve Object Using Hibernate:

The console will have these kinds of output according to our example. you can see that at last, we have User Name and Address

We can see in the console that hibernate is making a select statement to retrieve data from the table. The retrieved data can also be seen easily in the console. There are also other ways of retrieving data like HQL – Hibernate Query Language and directly SQL queries can also be written to retrieve data. We will see more details of it In the coming tutorials.

Summary:

In this tutorial, we have learned to retrieve the stored object from hibernate, for this purpose we have seen following things.

  • An example of retrieving objects with Hibernate.
  • Test run of that Example.

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About Author: Uzair Ahmed Khan is Technical Content Writer, Computer Software Blogger, and Software Engineer. He has a keen eye on Java Technologies other Related Developments.