Table of Contents
Introduction
Commonly known as Hibernate ORM (Object Relational Mapping), as the name suggests, Hibernate is an open-source mapper solution that was developed by Gavin King in 2001. It acts as a link between various objects in JAVA and Database Management systems. This is primarily the technique that is put to use when JAVA classes want to store or retain data from huge databases. In this article we will explore Hibernate Framework in JAVA in details.
What is the Hibernate Framework in JAVA?
As documented by Oracle the Hibernate framework in Java is a ‘high-performance Object-Relational Persistence and Query service’ that has been designed to facilitate data manipulation in large web-based applications. The word framework is used because a certain degree of abstraction is involved when using Hibernate. Coders can simply implement this framework into their programs without having to know the mechanism of the framework specifically. Some of the important operations like establishing connections with relevant databases and initiating query requests for CRUD (create, read, update, and delete) operations are automatically taken care of by the Hibernate Network. A keyword that is generally used with Hibernate is persistence logic. Persistence logic is a term used for storing data and updating information for long term use (usually tends to go as long as an application’s life). Hibernate Framework empowers independent persistence logic in JAVA.
Need of a Hibernate Framework
Hibernate Framework is used in JAVA as Java Database Connectivity (JDBC) often encounters problems when trying to process CRUD requests from applications. JDBC relies a lot on database dependency that is not always ideal for every purpose. It establishes unwanted coupling. Hibernate is also instrumental in strengthening ties between objects and databases. When it comes to handling runtime or compile-time exceptions, the framework does it for you, unlike JDBC. Also, there is a stark difference in readability between codes that use the framework and codes that do not. The framework provides for a proper structure and makes code reusable.
Hibernate Architecture
The Hibernate Architecture has multiple layers to it. The Java Application calls a persistence object that ultimately sends queries to the database and brings back the processed information to the application. The persistence object is made up of several other class objects as mentioned below:
- Configuration Object – It is responsible for two functions namely Database Connections (hibernate.properties and hibernate.cfg.xml) and Class Mapping Setup (the main link between objects and the database).
- SessionFactory Object – Used to create a SessionFactory object that is responsible for configuration changes.
- Session Object – This object is used every time an interaction is required between objects and the database.
- Transaction Object – These objects take care of the work performed during interactions.
- Query Object – These objects use SQL or HQL and retrieve data from DB.
- Criteria Object – If users have any criteria to retrieve objects or data from databases, these objects are created.
Advantages of Hibernate Framework in JAVA
XML files (hibernate.properties and hibernate.cfg.xml) are used to establish connections between objects and databases. Therefore, no code has to be written. It also provides a great deal of abstraction where developers are not required to know the nuances of SQL. As it is open-source and light-weight, this framework is widely used.
Example with Code
We will be dealing with a simple example where a system will store details of students. The database will include the last name and roll number of a student. //Let’s define the main class public class Student { private int roll_no; private String lastName; public int getRoll(){ return roll_no; } public void setRoll(int roll_no){ this.roll_no = roll_no; } public String getLastName(){ return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } //This example takes into consideration the .xml files have been written already for the program to //run. The class below will use the hibernate framework to store the objects. import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class StoreDB{ public static void main (String args []) { //Creating a ServiceRegistry object StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure(“hibernate.cfg.xml”).build(); Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory = meta.getSessionFactoryBuilder().build(); Session session = factory.openSession(); Transaction tran = session.beginTransaction(); Student s1 = new Student(); s1.setRoll(01); s1.setLastName(“Sharma”); session.save(s1); tran.commit(); System.out.println(“Stored in Database successfully”); factory.close(); session.close(); } }
0 Comments