Integrating Hibernate and jPOS Framework

Today i want to share about Integrating Hibernate and jPOS Framework.

The implementation will based on my old post about Implementation jPOS Transaction Manager you can read the jPOS project on these post.
The jPOS System is look like:
Integrating Hibernate and jPOS Framework

i will add new participant for FinancialTransaction group called FinancialTransactionLogging, the participant is responsible for inserting the FinancialTransactionQueryRemoteHost response into database using Hibernate framework.
First, For integrating Hibernate and jPOS, i create new qbean called HibernateManager that extends org.jpos.q2.QBeanSupport the initService methods will buildSessionFactory for init the hibernate SessionFactory object.

HibernateManager class will look like:

package com.didikhari.utility;

import java.io.File;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jpos.q2.QBeanSupport;
import org.jpos.util.NameRegistrar;

public class HibernateManager extends QBeanSupport {

	private SessionFactory sessionFactory;

	@SuppressWarnings("deprecation")
	@Override
	protected void initService() throws Exception {
		try {
			String hibernatePropsFilePath = cfg.get("hibernate_cfg_path");
			File hibernatePropsFile = new File(hibernatePropsFilePath);

			Configuration configuration = new Configuration(); 
			configuration.configure(hibernatePropsFile);
			sessionFactory = configuration.buildSessionFactory();
			NameRegistrar.register("hibernate-manager", this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void shutdown() {
		if (getSessionFactory() != null) {
			getSessionFactory().close();
		}
	}
}

and you need to create 60_hibernate_manager.xml file inside your deploy directory. and the xml file is:
<qbean name="hibernate-manager" logger="Q2" realm="hibernate-manager"
       class="com.didikhari.utility.HibernateManager">
       
	<property name="hibernate_cfg_path" value="\JPosISOServer\cfg\hibernate.cfg.xml" />
	

The hibernate configuration file, and inserting methods using Template Methods Pattern can be found here: Implementation Template Methods Design Pattern

The last thing i need to do is to create the FinancialTransactionLogging class implementation like this:

package com.didikhari.transaction.participant;

import java.io.Serializable;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Session;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.transaction.AbortParticipant;
import org.jpos.transaction.Context;

import com.didikhari.constant.Constant;
import com.didikhari.model.ResponseISO;
import com.didikhari.utility.HibernateUtil;
import com.didikhari.utility.TransactionCallAble;

/**
 * This class is responsible for log the message to the database
 * @author Didik Hari
 *
 */
public class FinancialTransactionLogging implements AbortParticipant {

	@Override
	public void abort(long id, Serializable context) { }

	@Override
	public void commit(long id, Serializable context) { }

	@Override
	public int prepare(long id, Serializable context) {
		try {
			ISOMsg respMsg = (ISOMsg) ((Context)context).get(Constant.RESPONSE_HOST);
			ResponseISO responseISO = new ResponseISO();
			responseISO.setMti(respMsg.getMTI());
			for (int i=2; i < 128; i++) {
				String prop = "bit"+ ISOUtil.zeropad(String.valueOf(i), 3);
				PropertyUtils.setProperty(responseISO, prop, respMsg.getString(i));
			}
			save(responseISO);
		} catch (Exception e) {
			e.printStackTrace();
			return ABORTED;
		}
		return PREPARED;
	}

	private void save(final ResponseISO responseISO) {
		HibernateUtil.doInTransaction(new TransactionCallAble<Integer>() {
			@Override
			public Integer call(Session session) throws Exception {
				session.save(responseISO);
				return null;
			}
		});
	}

	@Override
	public int prepareForAbort(long id, Serializable context) {
		return ABORTED;
	}

}

Thats all my post about Integrating Hibernate and jPOS Framework. Thank you for coming to my blog.

4 comments

Leave a Reply

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