Saving Object With Hibernate

Persist Object With Hibernate

In the previous tutorial, we have completed tasks related to setting up an environment for Hibernate.We have created a project and added hibernate libraries and JDBC driver. In this tutorial, we will understand objects with Hibernate in much more detail.

Storing Object without Hibernate

To store an object in the database if we are not using Hibernate,  we will do so with simple JDBC  drivers. For this purpose, we have to configure JDBC in the application. We need to write the model object, it is the object that we want to save. To store this user object we need to write the Class. We also have to instantiate and initialize the user object. So, we will write a service method. Before all this, we need to have the Database design. This design has tables and their relationship defined in it. Then finally we will write a DAO (Data Access Object) method to save the object using SQL queries. Steps are

  • Configure with JDBC
  • Write model object
  • Write service method
  • Database design
  • DAO method to store object

Storing the Object with Hibernate

In the case of Hibernate to store an object in the database, we still need JDBC. But this time, this JDBC is used by Hibernate to interact with the database. We will write an XML configuration file to configure JDBC with Hibernate. The model object will also be required to tell Hibernate that what needs to be stored. Here Service method will have the instance of the model object. This model object will then be passed to the hibernate API to save the model object. We don’t have to create a database design with all the tables. This will be managed by Hibernate. Finally, DAO method will not be needed here, it means no SQL queries to write.

  • Hibernate Configuration
  • Model object – Annotations
  • Service method uses Hibernate API to store objects directly to the database

Create Hibernate Configuration file

First, right-click on the project and select “File”

Start creating a new file
Start creating a new file

Now name the file as “hibernate.cfg.xml” which is the default name for Hibernate configuration file and select src directory to store it in src.

Name of Hibernate configuration file
Name of Hibernate configuration file

Add text into this empty created file like shown below and edit it with respect to your required database details. Here we have provided configuration for Postgres.

Here step one is complete that is Hibernate Configuration

Hibernate Configuration Explanation

In the above source, we have something called SQL Dialect you can search for dialect class name in the Hibernate Library In our example we have used PostgreSQLDialect  fully qualified name for it is “org.hibernate.dialect.PostgreSQLDialect”.

location of required hibernate dialect package
location of required hibernate dialect package
  • The option called show SQL which is set as true will print out all the SQL generated by Hibernate.
  • The hbm2ddl set as create means hibernate will create the table of our object
  • In Mapping class option we will list out all the model classes we create, for instance, we leave it as mentioned above.

Write Model Class

Now we will create a model class by right clicking on the project’s src directory as shown

Start creating Model Class
Start creating Model Class

Now, give a package name to the class and a class name as shown. Here we will use “UserDetails” as our class name and click “Finish

Give a pckage name and class name
Give a package name and class name

We will store a user name and an id. by creating two member variables and making them private and write getters and setters for it.

Then we will configure this Model Class in hibernate.cfg.xml like shown below

Above source in hibernate.cfg.xml will be replaced with this source

Now, the most important part comes which is the annotation part. Annotations will give some hints to the compilers. We will use annotations in our “UserDetails” class. The starting few lines of code will be edited like shown below.

@Entity in the above source is telling Hibernate to treat this class as an Entity and @Id is telling Hibernate to use Id as a primary key (databases concept).

Saving Object with Hibernate

Write Startup Class to initiate the process and make use of our created classes.

Again create a class by right clicking on the project’s src directory as shown

Start creating Model Calss
Start creating Model class

Give your class a name and a package name as shown. Here we used “HibernateMain” as our class name and also check “public static void main(String[] args)” and click “Finish”

Give a pckage name and class name
Give a package name and class name

Before Editing this created class and using hibernate API we have to learn how to use Hibernate API, there are certain steps evolved in it.

  • Create a session factory: using our hibernate.cfg.xml so we will be creating session factory which is the first step
  • Create a session from a session factory for CRUD operations.
  • Use created session object to save model objects.

Creating session factory is as simple as creating a simple object

We will then use this created factory object to start a new session. In this session we can save as many objects as we want. Source code for saving object is shown below.

Test Run:

We will use pgAdminIII Tool for PostgreSQL database initially there will be no tables inside our database.

Zero Tables Present in pgAdminIII
Zero Tables Present in pgAdminIII

Now run the project by right clicking on it and Select Run As –> java Application

Final Test Run of Hibernate Example
Final Test Run of Hibernate Example

Finally, in the pgAdminIII, there is a table with inserted values in it.

Table Created with Inserted values of Object
Table Created with Inserted values of Object

values are also inserted

Inserted values of the Object with Hibernate
Inserted values of the Object with Hibernate

Download Saving Objects with Hibernate Example Project

Summary:

In this tutorial, we have accomplished following things.

  • How to create Hibernate config file
  • How to create Model object
  • How to use sessions to store object

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.

Leave a Reply

Your email address will not be published. Required fields are marked *