Hibernate Examples - Hibernate

What is Hibernate Examples?

Let us attempt an instance of using Hibernate to provide Java perseverance in a separate application. We will go through different steps occupied in creating Java Application using Hibernate technology.

Create POJO Classes:

The primary step in creating an application is to construct the Java POJO class or classes, depending on the application that will be persisted to the database. Let us think our Employee class with getXXX and setXXX methods to make it JavaBeans obedient class.

A POJO (Plain Old Java Object) is a Java entity that doesn't expand or apply some specific classes and interfaces respectively necessary by the EJB framework. All normal Java substance is POJO.

When you create a class to be persisted by Hibernate, it's main to provide JavaBeans yielding code as well as one attribute which would work as index like id attribute in the Employee class.

Create Database Tables:

Second action would be creating tables in your database. There would be one table matching to each object you are willing to provide resolution. Consider above substance need to be stored and retrieved into the following RDBMS table:

Create Mapping Configuration File:

This movement is to form a mapping file that instructs Hibernate how to map the define class or classes to the database tables.

You should save the mapping article in a file with the arrange <class name>.hbm.xml. We saved our mapping article in the file Employee.hbm.xml. Let us observe little detail about the mapping document:

  • The mapping document is an XML document having <hibernate-mapping> as the root element which contains all the <class> elements.
  • The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.
  • The <meta> element is optional element and can be used to create the class description.
  • The <id> element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.
  • The <generator> element within the id element is used to automatically generate the primary key values. Set the class attribute of the generator element is set to native to let hibernate pick up identity, sequence or Hilo algorithm to create primary key depending upon the capabilities of the underlying database.
  • The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.

There are other attributes and elements accessible which will be used in a mapping certificate and I would try to cover as many as feasible while discussing other Hibernate related topics.

Create Application Class:

At last, we will create our application class with the main () method to run the application. We will use this application to save few Employees’ records and then we will apply CRUD operations on those records.

Compilation and Execution:

Here are the steps to accumulate and run the above mention application. Make sure you have set PATH and CLASSPATH correctly before arranged for the compilation and execution.

  • Create hibernate.cfg.xml configuration file as explained in configuration chapter.
  • Create Employee.hbm.xml mapping file as shown above.
  • Create Employee.java source file as shown above and compile it.
  • Create ManageEmployee.java source file as shown above and compile it.
  • Execute Manage Employee binary to run the program.

You would get following result, and records would be created in EMPLOYEE table.

If you check your EMPLOYEE table, it should have following records:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Hibernate Topics