EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Hibernate Framework

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Java Technology Tutorial » Hibernate Framework

Hibernate Framework

What is Hibernate Framework?

Hibernate is an open-source object-relational mapping (ORM) based java persistence framework. It is an ORM mapping tool in java. Hibernate is designed with the need to reduce complexity while connecting a relational database through java. Hibernate framework is designed to map java objects to implement object-oriented programming in the relational database. This is how to hibernate connects to the relational database to execute queries:

  • Hibernate connects directly with the specified database and uses hibernate query language (HQL) to execute queries and map query results to java objects.
  • Hibernate uses properties set in Hibernate configuration XML file to map query results to java objects.
  • The database connection is created using a session that helps in saving and fetching the persistent java object.
  • The session is created using the Session factory interface. In an ideal case, there should be only one session factory per database.

Comparison of Hibernate and JDBC

Here is a comparison table showing the difference in hibernate and JDBC:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

               Hibernate                       JDBC
Hibernate contains concrete classes which provide boilerplate logic. JDBC provides only interfaces and abstract classes.
All exceptions thrown by hibernating are unchecked. All classes in JDBC throw checked exceptions.
It does not require more resource management and does it internally. It requires more resource management like opening and closing of resources.
Stores java objects directly. It cannot store objects directly.
Supports database independent queries. Supports database-specific queries.
Supports Caching. It does not support caching.
Support lazy loading. It does not support lazy loading.

Hibernate Framework Architecture

Hibernate follows the layered architecture and has the following layers:

Hibernate Framework Architecture

  • Java application layer
  • Hibernate layer
  • Backend API Layer
  • Database Layer

Hibernate layer contains the following components which are as follows:

Hibernate Framework 3

1. Hibernate Configuration Object

This is the first object one has to create to establish database connection using hibernate. It should be ideally created once, during application initialization. The configuration object provides the following:

  • Database Connection: Database connection is established using one or more configuration files. The files are hibernated .properties and hibernate.cfg.xml.
  • Mapping: This creates a mapping between java classes and relational database tables.

2. Session Factory

Configuration object created in step 1 is used to create a session factory object which makes hibernate configuration ready using the provided configuration file and make way for session object to be created. Since the session factory is a heavy object, it is usually created once during the starting phase of the application. There is a need for multiple session factory object in case connections to multiple databases needs to be established. Also, the session factory is a thread-safe object.

3. Session

The session object establishes a physical connection with the database. It is a lightweight object and should be created each time interaction with the database is required. If an object needs to be persisted or needs to be retrieved, it can only be done using the session object. Session object should be closed as soon as the required operation is completed because they do not thread-safe.

Popular Course in this category
Java Training (40 Courses, 29 Projects, 4 Quizzes)40 Online Courses | 29 Hands-on Projects | 285+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.8 (9,026 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)JavaFX Training (1 Courses)JMeter Testing Training (3 Courses)

4. Transaction

It is an optional object and represents a unit of work done with the database. A transaction object ensures that either all operations must execute or none of them must execute. It is a single-threaded and short-lived object.

5. Query Object

This object uses structured query language (SQL) or Hibernate Query Language (HQL) to fetch data from the database and instantiate objects. A Query object can be used to limit output returned from the query, bind query parameters and execute the query.

Queries

Here we will execute some queries that will make things more clear. Let us consider an Entity Employee having class structured as:

Code:

Package com.edubca.hibernatetest;
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable
{
private static final long serialVersionUID = -1798070786993123455L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "empID")
private Integer empID;
@Column(name = "NAME")
private String empName;
@Column(name = "SALARY")
private Integer salary;
//Getters and setters
}

Hibernate requires an XML file called hibernate.cfg.xml that looks like:

Code:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.password"> edubca</property>
<property name="hibernate.connection.username">edubcauser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.edubca.hibernatetest.Employee"></mapping>
</session-factory>
</hibernate-configuration>

Below is the code to show how insertion and retrieval take place into the database using hibernate:

Code:

//Create Configuration object
Configuration con=new AnnotationConfiguration().configure(new File("hibernate.cgf.xml"));
// create session factory using configuration
SessionFactory fact=conf.buildSessionFactory();
//get session from session factory
Session session=fact.openSession();
//Instantiate and populate Employee entity object
Employee emp=new Employee();
emp.setempID(1);
emp.setempName(“Yash”);
emp.setSalary(40000);
Employee emp1=new Employee();
emp1.setempID(2);
emp1.setempName(“Aman”);
emp1.setSalary(42000);
//persist emp object
session.save(emp);
//persist emp1 object
session.save(emp1);
//retrieve data from database
Query query=session.createQuery(“from Employee”);
List<Employee> list= query.list();
For(Employee e : list){
System.out.println(“Employee with ID ” + e.getempID() + “ has Name ” + e.getempName() + “ has salary ” + e.getsalary());
}

Output:  

An employee with ID 1 has Name Yash has a salary of 40000.
An employee with ID 2 has Name Aman has a salary of 42000.

Conclusion

In this article, we have covered hibernate in detail, about its architecture, comparison with JDBC and code examples. We also noted that hibernate provides a simple and efficient way to interact with the database.

Recommended Articles

This is a guide to the Hibernate Framework. Here we discuss the architecture, components, and comparison of hibernate and JDBC with code examples. You may also look at the following articles to learn more –

  1. Frameworks In Java
  2. HADOOP Framework
  3. Hibernate Versions
  4. Lazy Loading in Hibernate

Java Training (40 Courses, 29 Projects, 4 Quizzes)

40 Online Courses

29 Hands-on Projects

285+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Hibernate Tutorial
  • Basic
    • What is Hibernate
    • What is Java Hibernate
    • Hibernate Versions
    • Hibernate Architecture
    • Hibernate Framework
    • Hibernate Tools
    • Hibernate Annotations
    • Hibernate Mapping
    • Hibernate Many to One
    • Hibernate Many to Many
    • Hibernate Dialect
    • Hibernate Session
    • Hibernate SessionFactory
    • Lazy Loading in Hibernate
    • Hibernate Criteria
    • Hibernate Persist
    • Hibernate EntityManager
    • Caching in Hibernate
    • Cascade in Hibernate
    • Hibernate Generator
    • Hibernate Validator
    • Spring Hibernate Integration
    • Hibernate Query Language
    • Hibernate Named Query
    • Hibernate Interview Questions
    • HQL
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Java Training (40 Courses, 29 Projects, 4 Quizzes) Learn More