« - »

The Approval Service: DB to POJOs (Phase I)

14 February 2008

In the interest of getting something accomplished and posted, I decided to limit my initial activity to simply working with the main RequestForApproval table, and not worry just yet about joining in the list of approvers or the audit history. We’ll save that for a second step. Still, there are quite a few new parts involved.

First of all, we need to create a table. I built mine in MySQL:

CREATE TABLE  'approval'.'requestforapproval' (
'id' int(11) NOT NULL auto_increment,
'type' varchar(255) default NULL,
'state' varchar(255) default NULL,
'payloadId' varchar(255) default NULL,
'payloadURI' varchar(255) default NULL,
'authorId' varchar(255) default NULL,
'authorURI' varchar(255) default NULL,
'authorName' varchar(255) default NULL,
'description' varchar(255) default NULL,
'dateTime' datetime default NULL,

After I created the table, I manually inserted one record using the same test data that I used for the sample RFA.

Next, I created a Hibernate mapping document for the RequestForApproval bean, which looks like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    <class name="org.restafarian.approval.beans.RequestForApproval"
        <id name="id">
            <generator class="native"/>
        <property name="type"/>
        <property name="state"/>
        <property name="payloadId"/>
        <property name="payloadURI"/>
        <component name="author">
	        <property name="id" column="authorId"/>
        	<property name="uri" column="authorURI"/>
        	<property name="name" column="authorName"/>
        <property name="description"/>
        <property name="dateTime" type="timestamp"/>


Between Java, Hibernate, and Betwixt, we now have three files for every bean in the data beans package:

Once the mapping document was completed, I built a Manager class to handle the CRUD actions on the RequestForApproval POJO, and called it RequestForApprovalManager:

package org.restafarian.approval.data;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.restafarian.approval.beans.RequestForApproval;

 * <p>RequestForApproval Manager.</p>
public class RequestForApprovalManager {
 private static final SessionFactory sessionFactory;
 private static Log log = LogFactory.getLog(RequestForApprovalManager.class);

 static {
  try {
   sessionFactory = new Configuration().configure().buildSessionFactory();
  } catch (Throwable e) {
   log.error("Initial SessionFactory creation failed: " + e, e);
   throw new ExceptionInInitializerError(e);

  * <p>Handles a get request for a single RFA.</p>
  * @param id the id of the requested record
  * @return the requested Request for Approval
 public static RequestForApproval getRequestForApproval(int id) {
  RequestForApproval rfa = null;

  Session session = sessionFactory.getCurrentSession();
  if (log.isDebugEnabled()) {
   log.debug("getRequestForApproval(" + id + ")");
  Transaction tx = session.beginTransaction();
  rfa = (RequestForApproval) session.get(RequestForApproval.class,
            new Integer(id));

  return rfa;

Finally, to get things to run, I had to import a number of dependent .jar files into the Example.ear file, set up a datasource on the application server, and throw in a hibernate configuration file to point to the datasource.

Oh, and one more thing: modify the RequestForApprovalServlet to use the RequestForApprovalManager to get the data:

RequestForApproval rfa = RequestForApprovalManager.getRequestForApproval(id);

Once that was done, all that was left was to test. I’d like to say that it worked the first time out and that was that, but it took just a few (OK, quite a few!) iterations to actually get to this point.

Next time, we’ll toss in the approvers and the audit log, and then I’ll post an updated .ear file.


Comments are closed.

Sorry, the comment form is closed at this time.