Open In App
Related Articles

Hibernate Example using JPA and MySQL

Improve Article
Save Article
Like Article

Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, Hibernate does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is a java framework that is used to develop persistence logic. More precisely Hibernate is an open-source, non-invasive, lightweight java ORM(Object-relational mapping) framework to develop objects which are independent of the database software and make independent persistence logic in all JAVA, JEE.

JPA stands for Java Persistence API. It is known as Java specification which provides some functionality and standard to ORM tools. It is used to control, review and persist data between Java objects and databases. It is regarded as the standard technique for Object Relational Mapping. Sometimes Geeks got confused between JPA and Hibernate. So here are some basic differences between JPA and Hibernate.



JPA is not an implementation. It is only a Java specification. Hibernate is an implementation of JPA.
JPA explains the handling of data in Java applications. Hibernate is an ORM (Object-Relational Mapping) tool that is used to save the Java objects in the database system.
As an object-oriented query language, JPA uses JPQL (Java Persistence Query Language) to execute database operations. As an object-oriented query language, it uses Hibernate Query Language (HQL) to execute database operations.

Implementation: We are going to build a simple Java application by creating a maven project and we are going to save some data inside the MySQL Database with the help of both Hibernate and JPA concepts. 

Step 1: Create a maven project in your favorite Java IDE (In this article we are using IntelliJ IDEA)

Step 2: When you have successfully created a maven project you have to add some dependencies in your pom.xml file. You have to add the following dependency in your pom.xml file.

Dependency for Hibernate is as follows:




Dependency for MySQL is as follows:





Example: pom.xml File



<?xml version="1.0" encoding="UTF-8"?>


Step 3: Create a simple POJO class and name the class as Song. 



// Java Program to Illustrate Creation of Simple POJO Class
// Importing required classes
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "song")
// POJO class
public class Song {
    @Id @Column(name = "songId") private int id;
    @Column(name = "songName") private String songName;
    @Column(name = "singer") private String artist;
    public int getId() { return id; }
    public void setId(int id) { = id; }
    public String getSongName() { return songName; }
    public void setSongName(String songName)
        this.songName = songName;
    public String getArtist() { return artist; }
    public void setArtist(String artist)
        this.artist = artist;


Step 4: Create a hibernate configuration file (XML file) inside the src > main > resources folder. Here we have named the file hibernate.cfg.xml. In this file, we are going to configure all the properties for the MySQL Database.


Example: hibernate.cfg.xml File 



<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <!-- Set URL -->
        <property name = "hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate-demo(your schema name)</property>
        <!-- Set User Name -->
        <property name = "hibernate.connection.username">your MySQL user name</property>
        <!-- Set Password -->
        <property name = "hibernate.connection.password">your MySQL password</property>
        <!-- Set Driver Name -->
        <property name = "hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name = "hibernate.show_sql">true</property>


Step 5: Create a class named App and inside the class write the main() method





// Java Program to Illustrate App File
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
// Main class
public class App {
    // Main driver method
    public static void main(String[] args)
        // Create Configuration
        Configuration configuration = new Configuration();
        // Create Session Factory
        SessionFactory sessionFactory
            = configuration.buildSessionFactory();
        // Initialize Session Object
        Session session = sessionFactory.openSession();
        Song song1 = new Song();
        song1.setSongName("Broken Angel");
        // Here we have used
        // save() method of JPA;


Step 6: Create a schema named hibernate-demo (you can choose your own) inside your MySQL Database. And run your application.




Output in MySQL Workbench

You can see the data has been saved inside your MySQL workbench. And in the hibernate-demo schema, a table named song has been created and the corresponding values for each column that you have set in class have been stored. 


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 21 Feb, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials