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:
[java]
package com.didikhari.util;
import org.hibernate.Session;
public interface TransactionCallAble
L call(Session session) throws Exception;
}
[/java]
After that, we need to create HibernateUtil class.
[java]
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;
@SuppressWarnings(“deprecation”)
private HibernateUtil(){
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public Session getSession() {
return sessionFactory.openSession();
}
public void closeSessionFactory(){
if (sessionFactory!=null) {
this.sessionFactory.close();
}
}
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
HibernateUtil hibernateUtil = new HibernateUtil();
Session session = hibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try {
T result = callable.call(session);
session.flush();
session.clear();
tx.commit();
return result;
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}finally{
session.close();
hibernateUtil.closeSessionFactory();
}
return null;
}
}
[/java]
And the hibernate.cfg.xml is like this:
[xml]
[/xml]
Then, create main class for test the function.
[java]
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
@Override
public Parameter call(Session session) throws Exception {
Parameter parameter = new Parameter();
parameter.setParamId(“NIM”);
parameter.setDescription(“Testing Insert”);
parameter.setValue(“113090009”);
parameter.setParamGroup(“Student”);
parameter.setOrdinal(0);
session.save(parameter);
return parameter;
}
});
System.out.println(“Insert data:”+result.getValue());
}
}
[/java]
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