Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:-

  1. Create the POJO /Persistent/Bean class

The encapsulation of many objects into a single object is done by POJO class or Bean class.

All the variables of bean class are set as private with public setter and getter methods, and it must contain a no-arg constructor.

Let us create Bean class- Student.java

2. Create a mapping file for Hibernate bean class

The format of the mapping class should be <classname>.hbm.xml file. For example, if our POJO class name is Student then the name of mapping class should be Student.hbm.xml. Hibernate mapping file contains many elements such as <hibernate-mapping></hibernate-mapping>, <class></class>, <id></id>, <generator></generator>, <property></property> and  many more.

Let us create a Student.hbm.xml mapping file.

3. Create a Configuration file

The name of the configuration file should always be hibernate.cfg.xml. It contains information about the mapping file and relational database.

Let us create a Configuration file named hibernate.cfg.xml

4. Create the Application class

It is a class which contains the main() methodused to run the application.

Let’s create Application.java.

Output

Database Table- student01

Pin It on Pinterest

Share This