Bergen & iBatis on the NetBeans Platform

Toni and I are visiting the Institute of Marine Research in Bergen, Norway, where a set of heavily data-centric applications (i.e., for processing data coming back from research cruises all over the world) are being integrated into a consolidated application on the NetBeans Platform.

A snapshot of the group to give a quick impression:

They're strongly in favor of using free & open-source solutions, of which the NetBeans Platform is obviously one. (In several cases, we're introducing them to NetBeans IDE for the first time, since several didn't know about the lightbulb hints in the IDE, while others hadn't used the Matisse GUI Builder before.) Another one is iBatis, which I had heard of but never used before. This gave me sufficient info and now I have a small CRUD (well, only "R" at this point) sample on the NetBeans Platform via iBatis (i.e., no JPA is being used, only a Derby database and iBatis XML files, with iBatis code in the TopComponent):

To give a quick intro to iBatis in the context of the NetBeans Platform, here's a screenshot of the application structure, followed by a brief description of the highlighted nodes below:

The highlighted files above:

  • Customer API. An API module that exposes two classes (generated in the IDE via the wizard, after which I deleted all the JPA annotations, leaving me with two POJOs representing rows in a database).

  • CustomerViewer. A module that provides the window that you see in the application, together with the two XML files typical to iBatis applications:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 
       "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
    <sqlMapConfig>
        <settings cacheModelsEnabled="true" enhancementEnabled="true" 
            lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" 
            useStatementNamespaces="false" />
        <transactionManager type="JDBC">
            <dataSource type="SIMPLE">
                <property name="JDBC.ConnectionURL" value="jdbc:derby://localhost:1527/sample"/>
                <property name="JDBC.Password" value="app"/>
                <property name="JDBC.Driver" value="org.apache.derby.jdbc.ClientDriver"/>
                <property name="JDBC.Username" value="app"/>
            </dataSource>
        </transactionManager>
        <sqlMap resource="Customer.xml" />
    </sqlMapConfig>
    <?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="Customer">
        <typeAlias alias="customer" type="org.customer.api.Customer"/>
        <select id="getAll" resultClass="customer">
                select \* from customer
        </select>
    </sqlMap>

    The above two do the same as persistence.xml in JPA, i.e., make a connection to the (in this case) Sample database in NetBeans IDE and retrieve the requested data.

  • DerbyClientLib. A single JAR (derbyclient.jar from the JDK) wrapped into a module and made available to the iBatis module.

  • iBatisLib. Two JARs from iBatis wrapped into a module and made available to the CustomerViewer module.

And how is the data retrieved by iBatis and shown in the window? Like this, in the TopComponent:

public CustomerTopComponent() {
    Reader reader = null;
    try {
        initComponents();
        setName(NbBundle.getMessage(CustomerTopComponent.class, "CTL_CustomerTopComponent"));
        setToolTipText(NbBundle.getMessage(CustomerTopComponent.class, "HINT_CustomerTopComponent"));
        add(new BeanTreeView(), BorderLayout.CENTER);
        reader = Resources.getResourceAsReader("SqlMapConfig.xml");
        SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        List<Customer> customers = (List<Customer>) sqlMap.queryForList("getAll", null);
        //And here we pass the retrieved customers into the factory that creates nodes:
        em.setRootContext(new AbstractNode(Children.create(new CustomerChildFactory(customers), true)));
        em.getRootContext().setDisplayName("Customers");
    } catch (SQLException ex) {
        Exceptions.printStackTrace(ex);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        try {
            reader.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    associateLookup(ExplorerUtils.createLookup(em, getActionMap()));
}

Here's the 'CustomerChildFactory' you see referenced above:

import java.beans.IntrospectionException;
import java.util.List;
import org.customer.api.Customer;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;

public class CustomerChildFactory extends ChildFactory<Customer> {

    private final List<Customer> customers;

    CustomerChildFactory(List<Customer> customers) {
        this.customers = customers;
    }

    @Override
    protected boolean createKeys(List<Customer> list) {
        list.addAll(customers);
        return true;
    }

    @Override
    protected Node createNodeForKey(Customer key) {
        try {
            return new BeanNode(key);
        } catch (IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
        }
        return null;
    }
    
}

And that's all. A simple example of iBatis usage on the NetBeans Platform. But I wonder why/when iBatis would be used rather than JPA?

Comments:

We have found iBatis to be the cleanest solution for integrating with legacy databases (where the structure of the database has evolved independently from an individual application). iBatis is particularly useful if your application code has significant interaction with stored procedures.

Posted by Jim Christie on April 12, 2010 at 07:42 AM PDT #

Jim,
how beautifully put. This is precisely the reason why we have choosen iBatis two years ago. I read two chapters in a Spring Framework textbook. One chapter was about Hibernate and one about iBatis and then we took the decision. Both Hibernate and iBatis are strong tools of course and my impression is that Hibernate has a larger number of users.

Posted by Trond Westgård on April 17, 2010 at 06:12 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today