RSS Feed for This PostCurrent Article

Load Data into Database without Creating Java Data Object

I am sure many of us are using ORM tools for Java to persist data into database. For most of the ORM tools, often that we need to create Data Object for each table in the database. E.g. when a new column is added in a database table, you need to create the corresponding field in your Java Data Object.

In my previous article, Use JPedal and JBoss Drools for Flexible PDF Extraction, I used JBoss Drools so that I can flexibly extract information from pdf file. In this case, I also need to have a flexible way to load the data into database. E.g., when there is one new field introduced in the pdf, all I need to do is creating the new database table column, modify some configuration file in my Java application, and that is it. I do not want to create or modify any Data Object and then recompile my Java application.

In this case, I also use JBoss Drools for my database loading. For the ORM tool, I use iBATIS. iBATIS Data Mapper framework makes it easier to use a database with Java and .NET applications. iBATIS couples objects with stored procedures or SQL statements using a XML descriptor. Simplicity is the biggest advantage of the iBATIS Data Mapper over object relational mapping tools.

E.g., have a look at the mapping file below:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//
DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="AUTHOR" >

  <insert id="insert_author" parameterClass="java.util.HashMap" >
    insert into AUTHOR (
      AUTHOR,
      EMAIL)
    values (
      #author:VARCHAR#,
      #email:VARCHAR#
      )
  </insert>

</sqlMap>

The parameterClass is java.util.HashMap. In order to invoke the insert operation, I only need to pass in the Java HashMap object.

By combining JBoss Drools and iBatis, I can have the flexibility to load data without creating any Data Object.

Map dataMap = new HashMap();
dataMap.put("author", "jason");
dataMap.put("email", "admin@twit88.com");

// Insert into database
sqlMapClient.insert("insert_author", dataMap);

Of course by programming this way, I lose the advantage of the “Domain Object” model, but in this case it fulfills my need of flexibility.


Trackback URL


Sorry, comments for this entry are closed at this time.