Implementation Template Methods Design Pattern

Today i want to share about Implementation Template Methods Design Pattern for hibernate session management.
Based on javapapers website, Template pattern is:
Template method design pattern is to define an algorithm as skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class.

you can read more about Template method design pattern on javapapers site.
This post will explain the Implementation Template Methods Design Pattern for hibernate session management. this methods is ensure that session is close after a jobs is done.
First, we need to create interface with generic object parameter called TransactionCallable. the Interface is look like below:

package com.didikhari.util;

import org.hibernate.Session;

public interface TransactionCallAble {
L call(Session session) throws Exception;

After that, we need to create HibernateUtil class.
package com.didikhari.util;

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

public class HibernateUtil{
private SessionFactory sessionFactory;

private HibernateUtil(){
sessionFactory = new Configuration().configure().buildSessionFactory();

public Session getSession() {
return sessionFactory.openSession();

public void closeSessionFactory(){
if (sessionFactory!=null) {

public SessionFactory getSessionFactory() {
return sessionFactory;

* Template Method that open session and close the session after job done!
* @author Danang Dj
* @param callable
* @return
public static T doInTransaction(TransactionCallAble callable){
HibernateUtil hibernateUtil = new HibernateUtil();
Session session = hibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
T result =;
return result;

} catch (Exception e) {
return null;


And the hibernate.cfg.xml is like this:

false oracle.jdbc.driver.OracleDriver test test jdbc:oracle:thin:@localhost:1521:xe org.hibernate.dialect.Oracle10gDialect true 10 50 3000 50 3000

org.hibernate.context.internal.ManagedSessionContext false false


Then, create main class for test the function.
package com.didikhari.main;

import org.hibernate.Session;

import com.didikhari.model.Parameter;
import com.didikhari.util.HibernateUtil;
import com.didikhari.util.TransactionCallAble;

public class Main {
public static void main(String[] args) {
Parameter result = HibernateUtil.doInTransaction(new TransactionCallAble() {

public Parameter call(Session session) throws Exception {
Parameter parameter = new Parameter();
parameter.setDescription(“Testing Insert”);
return parameter;

System.out.println(“Insert data:”+result.getValue());
And the result will printed like this:
Hibernate: insert into S_PARAMETER (DESCRIPTION, ORDINAL, PARAM_GROUP, VALUE, PARAM_ID) values (?, ?, ?, ?, ?)
Insert data:113090009

And thats all my post about Implementation Template Methods Design Pattern, thank you for comming to my blog.

Leave a Reply

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