Hibernate persist Method

In this tutorial you will learn about how to use persist method in hibernate.



How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

Hibernate persist Method

In this tutorial you will learn about how to use persist method in hibernate.

Hibernate's persist() method is used to insert / save the record to the database table. This method is equal to the save() method but, it is different in the case that the save() method can return the id (primary key) value that is generated by the hibernate but the persist() method never returns value to the client.

Example :

Here is a basic example which will demonstrate you how persist() method can be used in Hibernate. For this I am giving a complete source code which can be downloaded. To achieve the solution of our problem at first I have created a table in MySQL named employee with the fields Id, Name and then created a POJO / Persistent class named Employee.java to fetch the persistent object, a hibernate.cfg.xml file that the Hibernate can utilize it to create connection pool and the required environment setup, an employee.hbm.xml file to map an Employee object with table employee. And finally create a simple java file using which we will use persist() method of Hibernate to insert record to the table.

Complete Code :


package roseindia;

public class Employee {
private int id;
private String name;

public Employee()

public Employee(String name)
this.name = name;
public int getId() {
return id;
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;


<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="roseindia">
<class name="Employee" table="employee">
<id name="id" type="int" column="Id" >
<generator class="native"/>
<property name="name">
<column name="Name" />


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>



package roseindia;

import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class EmpPersist {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String args[]) {

Session session =null;

Configuration cfg = new Configuration().addResource("roseindia/employee.hbm.xml").configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
catch(Throwable th)
System.err.println("Failed to create sessionFactory object."+ th);
throw new ExceptionInInitializerError(th);
session = sessionFactory.openSession();
Employee employee = new Employee();
Transaction tx = session.beginTransaction();
Scanner scan = new Scanner(System.in);
System.out.println("Enter name of Employee : ");
String setEmpName = scan.next();
System.out.println("Data has been inserted to the table.");
catch(Exception e)

Output :

1. Table without record

2. When you will execute the java file EmpPersist.java and provide the information looking as on console you will get the output as :

3. When you will provide the info looking at console successfully then the data will be inserted to the table and will look as :

Download Source Code


Share on Google+Share on Google+

Hibernate persist Method

Posted on: February 24, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate persist Method  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image