Thursday, December 24, 2015

Hibernate 5 Maven example


Here in this post we will be looking into the basic setup of "Hibernate 5".
Hibernate 5 is being recently released to explore its feature we started setting up work space and the desired libraries needed for "Hibernate 5".

Here in this post we will be using
  1. Eclipse Mars.1 Release (4.5.1)
  2. Maven
  3. JDK 1.8
  4. Hibernate 5
  5. MySQL
You can also download/clone the complete workspace from GitHub : -

If you are not able to clone the repository than download the complete workspace and import in eclipse using following instruction 

File - > Import - > General - > Existing project into workspace

Maven Dependencies for Hibernate 5 

Here in this post we will try to insert an employee entry into the "employee" table which we have created.
Next step is to create an "Employee" @Entity

package com.hibernate.tutorial.entity;

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

@Table(name = "employee")
public class Employee {

 @Column(name = "id")
 Long id;

 String employeeName;

 String employeeAddress;

 public Employee(Long id, String employeeName, String employeeAddress) { = id;
  this.employeeName = employeeName;
  this.employeeAddress = employeeAddress;

 public Employee() {


 public Long getId() {
  return id;

 public void setId(Long id) { = id;

 public String getEmployeeName() {
  return employeeName;

 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;

 public String getEmployeeAddress() {
  return employeeAddress;

 public void setEmployeeAddress(String employeeAddress) {
  this.employeeAddress = employeeAddress;


Next we need to write a code to insert a record into the table. Refer to following code where we have created following instances

  1. SessionFactory 
  2. Session
  3. Transaction
package com.hibernate.tutorial.mainclass;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.tutorial.entity.Employee;

public class Hibernate5InsertTest {

 public static void main(String[] args) {
  SessionFactory sessionFactory;
  sessionFactory = new Configuration().configure().buildSessionFactory();

  Session session = sessionFactory.openSession();

  Transaction tx = session.beginTransaction();

  Employee emp = new Employee();
  emp.setId(new Long(1));
  emp.setEmployeeName("Rahul Wagh");
  emp.setEmployeeAddress("Indore, India");;

SQL Script for "employee" table  : -

CREATE TABLE employee(
employee_name VARCHAR(100) NOT NULL,
employee_address VARCHAR(40) NOT NULL,
 PRIMARY KEY ( id));
Hope this article will help you to setup your Hibernate 5 Workspace.

For any issue please post your comments and leave your feedback

1 comment:

  1. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.