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 "-// SQL Map Config 2.0//EN" 
        <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"/>
        <sqlMap resource="Customer.xml" />
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap PUBLIC "-// SQL Map 2.0//EN" 
    <sqlMap namespace="Customer">
        <typeAlias alias="customer" type="org.customer.api.Customer"/>
        <select id="getAll" resultClass="customer">
                select \* from customer

    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 {
        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)));
    } catch (SQLException ex) {
    } catch (IOException ex) {
    } finally {
        try {
        } catch (IOException 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;

    protected boolean createKeys(List<Customer> list) {
        return true;

    protected Node createNodeForKey(Customer key) {
        try {
            return new BeanNode(key);
        } catch (IntrospectionException 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?


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 #

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 #

Unfortunately, ibatis/mybatis leads to objects that are nothing but empty. In other words they are just structs in the old C/C++ world. I have seen an application declare an object and never ever use a set method. You scratch your head and then it becomes obvious that inserts and updates do it for you behind the scenes. I'd not recommend mybatis since it will lead to the same maintenance nightmare as of a legacy application. Better redesign the database and build it right using JPA.

Posted by guest on April 24, 2015 at 08:27 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« February 2017