Share on Google+Share on Google+

Hibernate : Session Save

In this section we will discuss how works in Hibernate.

Hibernate : Session Save

In this section we will discuss how works in Hibernate.

Hibernate :

save() method is used for saving the object of your persistent class into database table.It inserts object and fails if primary key already exist. returns the generated identifier (Serializable object)

Syntax :  save(Object object) throws HibernateException

Parameters: object - a transient instance of a persistent class

Returns: generated identifier

Throws: HibernateException

Example : In this example we are saving employee record by using method.

Here is main class code -

package net.roseindia.main;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateSessionSave {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Employee employee = new Employee();
System.out.println("Employee record saved");
} catch (HibernateException e) {
} finally {

Output :

Hibernate: insert into employee (date_of_join, name, salary) values (?, ?, ?)
Employee record saved

Click here to download complete source code


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

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate : Session Save  

Post your Comment

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