How to Save Object to Database using Hibernate Framework

Save Object to Database using Hibernate

In this tutorial, I am attaching simple program about, How to Save Object to Database using Hibernate Framework. The example program were tested with environment and output has been shared in the same post.

Environment Details

To run this example provided, Make sure that you have environment with followings.

  1. JDK 1.7
  2. Hibernate 4.3.6 Distribution
  3. MySQL Database
  4. Eclipse IDE

Project Structure

This example tutorial developed using Hibernate 4.3.6 API. I have provided the link to download distribution below

Download Hibernate 4.3.6 Final Distribution here

Save Object to Database using Hibernate

Once you downloaded the Hibernate Distribution, You can copy the above jar files (ie: listed in the above screen shot) to eclipse project folder, Which you can find under lib folder of distribution.

Employee.java

The Employee (ie: POJO) class should be annotated with @Entity and @Table, In order to Save Object to Database using Hibernate framework. The @Id annotation representing the Primary Key for the table, which is mandatory in hibernate framework. In other word each POJO class should have the Primary Key mapping with @Id annotation.

package com.dineshkrish.hibernate;

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

@Entity
@Table
public class Employee {

	@Id
	private int empId;
	private String empName;
	private double empSalary;
	private String empDepartment;

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public double getEmpSalary() {
		return empSalary;
	}

	public void setEmpSalary(double empSalary) {
		this.empSalary = empSalary;
	}

	public String getEmpDepartment() {
		return empDepartment;
	}

	public void setEmpDepartment(String empDepartment) {
		this.empDepartment = empDepartment;
	}

}

SessionProvider.java

This class responsible to provide the Session object whenever its required.

package com.dineshkrish.hibernate;

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

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class SessionProvider {

	private static Session session;
	private static Configuration configuration;
	
	private SessionProvider() { }
	
	static {
		
		configuration = new Configuration()
	    
		.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
	    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
	    .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/employee")
	    .setProperty("hibernate.connection.username", "root")
	    .setProperty("hibernate.connection.password", "")
	    .setProperty("hibernate.show_sql", "true")
	    .setProperty("hibernate.hbm2ddl.auto", "update")
	    
	    // Your Mapping Class
	    .addAnnotatedClass(com.dineshkrish.hibernate.Employee.class);
	}
	
	
	public static Session getSession() {
	
		SessionFactory factory = configuration.buildSessionFactory();
		session = factory.openSession();
		return session;
	}
	
}

EmployeeApplication.java

package com.dineshkrish.hibernate;

import org.hibernate.Session;

public class EmployeeApplication {

	public static void main(String[] args) {
		
		// Getting the Session Object 
		Session session = SessionProvider.getSession();
		
		session.beginTransaction();
		
		// Defining Employee Object and Setting Properties
		Employee employee = new Employee();

		employee.setEmpId(101);
		employee.setEmpName("Dinesh Krishnan");
		employee.setEmpSalary(30000);
		employee.setEmpDepartment("Development");
		
		// Saving the Object to Database
		session.save(employee);
		
		session.getTransaction().commit();
		
		session.close();
		
	}
}

Table Screen Shot

Save Object to Database using Hibernate

References

1. Hibernate API Documentation
2. How to get Hibernate Session Object