Let us see how to develop our own Hibernate Template class.
HibernateTemplate.java
HibernateUtil.java
HibernateTemplate.java
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.varasofttech.util.HibernateUtil;
public class HibernateTemplate {
private SessionFactory sessionFactory = null;
public HibernateTemplate() {
sessionFactory = HibernateUtil.getSessionFactory();
}
public Serializable save(Object entity) {
Serializable id = null;
Transaction transaction = null;
Session session = sessionFactory.openSession();
try {
transaction = session.beginTransaction();
id = session.save(entity);
transaction.commit();
} catch (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return id;
}
public Object update(Object entity) {
Transaction transaction = null;
Session session = sessionFactory.openSession();
try {
transaction = session.beginTransaction();
session.update(entity);
transaction.commit();
} catch (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return entity;
}
public Object findById(Serializable id, Class<?> clas) {
Session session = sessionFactory.openSession();
return session.get(clas, id);
}
public void delete(Serializable id, Class<?> clas ) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Object data = findById(id, clas);
session.delete(data);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public List<?> findAll(Class<?> clas) {
Session session = sessionFactory.openSession();
Query query = session.createQuery( "from " + clas.getName());
return query.list();
}
}
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
return configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static void closeSessionFactory() {
if(sessionFactory != null) {
sessionFactory.close();
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
0 comments:
Post a Comment