Wednesday, May 7, 2008

Using Middlegen on iSeries/AS400

What is Middlegen?

Middlegen is an open source code generation framework that provides a general-purpose database-driven engine using various tools such as JDBC, Velocity, Ant and XDoclet.


Create a working directory where you install and store all related files. In this tutorial we'll use C:\Java as working directory. If you want to store it somewhere else, then you'll have to replace every occurrence of "C:\Java" throughout the tutorial by the desired directory.

What is Ant?

Apache Ant is a Java-based build tool.Currently, the only way to start Middlegen is from Ant. The middlegen.jar file contains an Ant task you should use to run Middlegen.

Download and install Ant

1.Surf to the Ant download page.
2.Pick the Ant binary (For this tutorial apache-ant-1.6.2 is used)
3.Unzip Ant binary to C:\Java
4.Rename apache-ant-1.6.2 to ant (C:\Java\ant)
5.Install Ant on Windows
Assume Ant is installed in c:\Java\ant\. The following sets up the environment:

set ANT_HOME=c:\Java\ant
set JAVA_HOME=c:\Java\jdk1.5
set PATH=%PATH%;%ANT_HOME%\bin

6.Run ant in command prompt
Type ant, you should get a msg 'build.xml does not exist!' This means you are ready to use ANT now.

What is the relationship to Hibernate?

The Middlegen design includes a plug-in architecture which facilitates particular aspects of a target system. Existing plugins include, simple java classes, EJB, JDO and now (close to) complete support for Hibernate. The hibernate plugin focuses on the generation of the mapping configuration files. Hibernates' excellent native hbm2java tool can be used to generated the domain classes.

Download and install Middlegen-Hibernate plugin

1.Surf to Middlegen-Hibernate plugin download page
2.Pick hibernate-middlegen
3 Unzip to C:\Java Now it is C:\Java\Middlegen-Hibernate-r5
4.Using command prompt change directory to C:\Java\Middlegen-Hibernate-r5 and type ant
5.A gui opens to show tables in hsqldb

6.Click Generate to generate metadata-mapping files

Middlegen-Hibernate plugin directory structure

The Middlegen-Hibernate plugin consists of the following directory layout:

+--- build // contains the generated source
| +--- gen-src // contains the built mapping-metadata files
+--- lib // contains necessary dependencies
+--- config // contains database mapping
| +--- database // contains various database mapping xml files
+---- middlegen-lib // contains Middlegen-Hibernate plugin jars
+--- src
| +--- sql // contains SQL scripts
|--- build.xml // Ant build file
|--- // defines build properties

Configuring Middlegen-Hibernate plugin
1.On iSeries build a test Schema HMP.
2.Place jt400.jar in lib.
3.Build database mapping file as db2_as400.xml

4.Build SQL script file as airline-db2_as400.sql

5.Modify build.xml
5.1 Replace db2_as400.xml in doctype declaration

5.2 Remove catalog

6.Rename to
7.Uncomment database.script.file and replace

Running Middlegen-Hibernate plugin

1.Open command prompt as C:\Java\Middlegen-Hibernate-r5 and type ant create-table
2.A message SQL statements executed successfully is shown.
3.Now type ant.
2.A gui is shown listing tables.
3.Click Generate to generate metadata-mapping.
4.Mapped-metadata files are generated under C:\Java\Middlegen-Hibernate-r5\build\gen-src.

Product                                                Version

DB2 UDB for AS/400                     05.04.0000 V5R4m0
Ant                                                        1.6.1
Hibernate Core                                  hibernate-2.1.7c
Hibernate Extension                        hibernate-extensions-2.1.3


Middlegen-Hibernate plugin

Play with Middlegen

Friday, May 2, 2008

Using Hibernate with iSeries/AS400

Configuring hibernate.cfg.xml

Hibernate version : 3.2.6 GA


Implementation-Version: JTOpen 3.3

database ASDB
properties naming=system;libraries=QTEMP ASDB ASDBA SFG

Complete URL

libraries=QTEMP ASDB ASDBA SFG;translate binary=true;prompt=false;user=GUEST;

Note : properties are separated by ';'.

Download jt400.jar

A list of supporting dialect

Supporting dialect

Play with iseries........!