Java Developer – Hibernate Hands-On

What is Hibernate in JAVA?

Hibernate is a high-performance Object/Relational persistence and query service, which is licensed under the open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities. This tutorial will teach you how to use Hibernate to develop your database based web applications in simple and easy steps.

What is JDBC?

JDBC stands for Java Database Connectivity. It provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database.

JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification.

Pros and Cons of JDBC

Pros of JDBCCons of JDBC
Clean and simple SQL processingGood performance with large dataVery good for small applicationsSimple syntax so easy to learnComplex if it is used in large projectsLarge programming overheadNo encapsulationHard to implement MVC conceptQuery is DBMS specific
Click here to read more java interview Questions and Answers

Click here to read more general programming solution

Why Object Relational Mapping (ORM)?

When we work with an object-oriented system, there is a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects.

Consider the following Java Class with proper constructors and associated public function −

public class Employee {
   private int id;
   private String first_name; 
   private String last_name;   
   private int salary;  

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   public int getId() {
      return id;
   public String getFirstName() {
      return first_name;
   public String getLastName() {
      return last_name;
   public int getSalary() {
      return salary;

Consider the above objects are to be stored and retrieved into the following RDBMS table −

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,

First problem, what if we need to modify the design of our database after having developed a few pages or our application? Second, loading and storing objects in a relational database exposes us to the following five mismatch problems −

Sr.No.Mismatch & Description
1GranularitySometimes you will have an object model, which has more classes than the number of corresponding tables in the database.
2InheritanceRDBMSs do not define anything similar to Inheritance, which is a natural paradigm in object-oriented programming languages.
3IdentityAn RDBMS defines exactly one notion of ‘sameness’: the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).
4AssociationsObject-oriented languages represent associations using object references whereas an RDBMS represents an association as a foreign key column.
5NavigationThe ways you access objects in Java and in RDBMS are fundamentally different.

The Object-Relational Mapping (ORM) is the solution to handle all the above impedance mismatches.

What is ORM?

ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C#, etc.

An ORM system has the following advantages over plain JDBC −

1Let’s business code access objects rather than DB tables.
2Hides details of SQL queries from OO logic.
3Based on JDBC ‘under the hood.’
4No need to deal with the database implementation.
5Entities based on business concepts rather than database structure.
6Transaction management and automatic key generation.
7Fast development of application.

An ORM solution consists of the following four entities −

1An API to perform basic CRUD operations on objects of persistent classes.
2A language or API to specify queries that refer to classes and properties of classes.
3A configurable facility for specifying mapping metadata.
4A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks

There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.

  • Enterprise JavaBeans Entity Beans
  • Java Data Objects
  • Castor
  • TopLink
  • Spring DAO
  • Hibernate
  • And many more

Welcome to Java Developer – Hibernate

Hibernate Problem Hands-on:-

You have to complete the two Bean classes, student and Laptop.

Create two tables, namely student and laptop using Hibernate(The Hibernate config file has already been created.)

Use OnetoMany and ManytoOne relationships, where one student can have many laptops. Using the relationship, create a new “student_id” Column in the laptop table that has the studend ID.

Uses H2 Database

Click here to read our other article


File Name – hibernate.cfg.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE hibernate-configuration PUBLIC

        “-//Hibernate/Hibernate Configuration DTD 3.0//EN”




      <property name=”connection.driver_class”>org.h2.Driver</property>

        <property name=”connection.url”>jdbc:h2:mem:test</property>

        <property name=”connection.username”>sa</property>

        <property name=”connection.password”></property>

        <property name=”connection.pool_size”>1</property>

        <property name=”dialect”>org.hibernate.dialect.H2Dialect</property>

        <property name=”show_sql”>true</property>

        <property name=”current_session_context_class”>thread</property>

        <property name=””>create-drop</property>

        <property name=”hibernate.dbcp.initialSize”>5</property>

        <property name=”hibernate.dbcp.maxTotal”>20</property>

        <property name=”hibernate.dbcp.maxIdle”>10</property>

        <property name=”hibernate.dbcp.minIdle”>5</property>

        <property name=”hibernate.dbcp.maxWaitMillis”>-1</property>

        <mapping class=”com.fresco.DemoHib.Student” />

        <mapping class=”com.fresco.DemoHib.Laptop” />



AVvXsEg5Mimw6huP7v5ye7C mgH YeSGB0cmJMLR0xKztNvWWfiOGYenS VwTdBLJM9GOhNfc9YTOUBMUbGHFpwA6jcSS5qQCYiCxjIX0bkTDXHVW3uIgvLwXonFAOYprCnlA btRxJEANaka83gcK4k0S1JOMTO6oE27diJnab SSp5WpDEgY7FSYE3pERn=w640 h330

AVvXsEjT0nPbsvE7A1z8EXt4HHgQ4OzZhwCm M FVLO8 73uBxZZkDSM GBvPGW9ahP kS FhwHMmOXyICMIMmBT2VbmyLsno0ACUaUDud5 7pbFD8jUiarXsigvQhrQYV47OE8BRTS 0XIvncRG4EcpZAesRJPpHYGljhMTCsjocXvte7pE4sTm74CeXxeK=w640 h334

SAP S/4HANA Master Data Concepts Questions & Answers
SAP S/4HANA Functional Capabilities 1909 Questions & Answer
SAP S/4HANA Technical Capabilities 1909 Questions & Answers
SAP Implementation S/4HANA Project Management Questions & Answers
SAP S/4 HANA Enterprise Management – Sales Questions Answers
SAP S/4HANA Key Concepts Overview Questions & Answers
SAP S/4HANA Enterprise Structure Questions & Answer
SAP S/4HANA Reporting and Analytics Questions & Answers
SAP S/4HANA Navigation Interview Questions and Answers
SAP S/4HANA Awareness 1909 Questions and Answers

About Author

After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

2 thoughts on “Java Developer – Hibernate Hands-On”

  1. This is a useful article. The Way of writing is good. By reading this, I surely can enrich my knowledge. Your idea is really outstanding. Thank’s to share us such an important thing.

Leave a Comment