Handle Transaction Hibernate example

Handle Transaction Hibernate example

To handle transaction in hibernate it offers wrapper API called Transaction which translates calls into native transaction system of deployment environment. Below is demo java program to show transaction and exception handling:

  • Demo program to handle transaction
package com.javahonk;

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

import com.javahonk.bean.Child;
import com.javahonk.bean.Mother;
import com.javahonk.util.HibernateUtil;

public class HandleTransactionNonManaged {
    
    static SessionFactory sessionFactory = 
            HibernateUtil.getSessionFactory();  

    public static void main(String[] args) {
        
        Session session = null;
        Transaction transaction = null;
        
        try {
            
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            
            //Sample insert data for demo
            Mother mother = new Mother();
            mother.setFirstName("Mary");
            mother.setLastName("Kay");
            mother.setCity("Edison");
            mother.setZip("10038");
            mother.setState("NJ");
            session.save(mother);
            
            Child child = new Child();
            child.setFirstName("Tom");
            child.setLastName("Kay");
            child.setMother(mother);
            session.save(child);
            
            Child child2 = new Child();
            child2.setFirstName("John");
            child2.setLastName("Kay");
            child2.setMother(mother);
            session.save(child2);
            
            transaction.commit();
            
            
        } catch (RuntimeException runtimeException) {
            
            if (null != transaction) {
                transaction.rollback();
            }else {
                throw runtimeException;
            }
            
        }finally{
            
            if (null != session) {
                session.close();
            }           
        }

    }

}

 

  • Much flexible solution using Hibernate built-in feature current session context management:
package com.javahonk;

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

import com.javahonk.bean.Child;
import com.javahonk.bean.Mother;
import com.javahonk.util.HibernateUtil;

public class HandleTransactionUsingCurrentSession {
    
    static SessionFactory sessionFactory = 
            HibernateUtil.getSessionFactory();  

    public static void main(String[] args) {
        
        Session session = null;
                
        try {
            
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.getCurrentSession();
            session.beginTransaction();
            
            //Insert data example
            Mother mother = new Mother();
            mother.setFirstName("Mary");
            mother.setLastName("Kay");
            mother.setCity("Edison");
            mother.setZip("10038");
            mother.setState("NJ");
            session.save(mother);
            
            Child child = new Child();
            child.setFirstName("Tom");
            child.setLastName("Kay");
            child.setMother(mother);
            session.save(child);
            
            Child child2 = new Child();
            child2.setFirstName("John");
            child2.setLastName("Kay");
            child2.setMother(mother);
            session.save(child2);
            
            session.getTransaction().commit();
            
            
        } catch (RuntimeException runtimeException) {
            
            if (null != session) {
                session.getTransaction().rollback();
            }
        }

    }

}

That’s it Handle Transaction Hibernate example for more information please see this hibernate official tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *