Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package abstracts; /** * * @author qq */ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public abstract class EntityBase { protected SessionFactory factory; protected Session session; protected Transaction transaction; public final void connect() { factory = new Configuration().configure().buildSessionFactory(); session = factory.openSession(); transaction = session.beginTransaction(); } public final void disconnect() { if (!transaction.wasCommitted()) { transaction.commit(); } if (session.isOpen()) { session.close(); } } }