Hibernate Transaction

In this tutorial you will learn about the Transaction in Hibernate.

Ads

Tutorials   
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
Ads

Hibernate Transaction

In this tutorial you will learn about the Transaction in Hibernate.

In transaction multiple operations are gathered into a single unit of work. On failure of operation in the batch Hibernate throws exceptions, any exceptions, are FATAL. In this case transaction is need to be rolled back and the unit of work is stopped as well as the the current session is closed instantly. Hibernate supports several notions of transactions such as JDBC transactions, Java Transaction API (JTA). Few application servers and standalone applications supports only JDBC transactions and few supports only the JTA.

Hibernate provides a way to abstract the transaction management from the environment by implementing an interface Transaction. Each transaction is managed within a Session and it is started by calling a method Session.beginTransaction(). This interface provides the various methods some of them are as follows :

  • begin()
  • commit()
  • rollback()
  • getTimeout()

Example :

Here I am giving simple code as example that will demonstrate how may you use the Hibernate Transaction.

package roseindia.net;

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

public class PersonDetail
{
private static SessionFactory sessionFactory = null;
public static void main(String[] args)
{ 
Session session = null;
Transaction tx = null;
try 
{
try
{
PersonFactory factory = new PersonFactory();
sessionFactory = factory.getSessionFactory();
session = sessionFactory.openSession();
Person person = new Person();
System.out.println("Inserting Record");
tx = session.beginTransaction();
person.setId(1);
person.setName("Roseindia");
session.save(person);
tx.commit();
System.out.println("Done");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
finally
{
session.close();
}
}
}

 

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Transaction

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

Advertisements

Ads
 

Discuss: Hibernate Transaction  

Post your Comment


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

Ads

 

Ads