Home Hibernate Hibernate Session



Hibernate Session
Posted on: July 9, 2012 at 12:00 AM
This section contains the explanation of hibernate session.

Hibernate Session

This section contains the explanation of  hibernate session.

Hibernate Session management:

Session interface is defined in org.hibernate.Session.It plays very important role in databases connection.
It is constructed by Hibernate to get a connection with a database. It is light weight and run for a short time. You can open session and perform operations as create, read and delete until the session is closed. Hibernate loads database object ,which is associated with the session. You can save and retrieve persistent object through a session object.

Example :

Here is an example showing how to open and close session.

package net.roseindia.main;

import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession(); //session is opened
Transaction transaction = null;
try {

transaction = session.beginTransaction();
String sql = "insert into student(roll_no, name,course) values(12, 'Roxi' ,'Hibernate')";
Query query = session.createSQLQuery(sql); //using session for createSQLQuery
query.executeUpdate(




					);
System.out.println("New record inserted");
session.getTransaction().commit();
session.flush();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close(); //session close
}
}
}

Related Tags for Hibernate Session:


More Tutorials from this section

Ask Questions?    Discuss: Hibernate Session  

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.