How to insert data into database using hibernate

hibernate is the most famous tool that makes our java application database independent. in this hibernate example, let’s create a simple java application to create a connection with MySQL Database and insert data into MySQL table using hibernate.

Major steps to insert data in hibernate

  1. Add dependency for Hibernate and MySQL connector in the project(pom.xml)
  2. Create Hibernate configuration file (hibernate.cfg.xml)
  3. Create Entity Class
  4. Configure Session Factory and Session and start the transections

Add dependency for Hibernate

Below is the maven dependency that you need to add in the pom.xml 


<!-- -->

Create hibernate configuration file (hibernate.cfg.xml)

 create an XML file and keep this file in the src folder also the name “hibernate.cfg.xml” is not important to be the same but yes to follow the standards will recommend keeping the same name.

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

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_tutorial</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>

Create Entity Class

Entity classes is a simple Java class where we will use some annotation that makes it entity class to map with a database table.

@Entity annotation is used to make the classes an entity.

@Table annotation is used to map the class with the database table name like @Table(name = “users”)

@Column annotation is used to map the column name with fields like @Column(name = “id”)

package com.loginexample.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "users")
public class Users {

 @Column(name = "id")
 private long id;
 @Column(name = "username")
 private String username;
 @Column(name = "password")
 private String password;
 @Column(name = "firstName")
 private String firstName;
 @Column(name = "lastName")
 private String lastName;

 public Users(String username, String password, String firstName, String lastName) {
  this.username = username;
  this.password = password;
  this.firstName = firstName;
  this.lastName = lastName;

 public long getId() {
  return id;

 public void setId(long id) { = id;

 public String getUsername() {
  return username;

 public void setUsername(String username) {
  this.username = username;

 public String getPassword() {
  return password;

 public void setPassword(String password) {
  this.password = password;

 public String getFirstName() {
  return firstName;

 public void setFirstName(String firstName) {
  this.firstName = firstName;

 public String getLastName() {
  return lastName;

 public void setLastName(String lastName) {
  this.lastName = lastName;

Insert data into database using hibernate

Create a java main class ( ) and create an object of SessionFactory and configure the session

SessionFactory factory = new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(Users.class) .buildSessionFactory(); Session session = factory.getCurrentSession();

Create an object of Entity class(users), begin the transaction, Add data object into the database, Commit the transaction, and print response message.

package com.loginexample.utility;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.sql.ordering.antlr.Factory;

import com.loginexample.entity.Users;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  SessionFactory factory = new Configuration()
  Session session = factory.getCurrentSession();

  try {			
   Users user = new Users("username", "password", "firstName", "lastName");
   System.out.println("Data is Added");
  } finally {
   // TODO: handle finally clause