more of an aspiration than a claim

Middlegen Tutorial: Quick Start Guide

This article is a brief description of Middlegen’s capabilities and a quick start guide is also provided. The tutorial details the steps I took to modify the provided sample configuration to run with my own database for use with Hibernate.

Middlegen Overview

Middlegen is a database driven code generator that is run and configured with Ant scripts. A UML like GUI front-end configuration tool is also provided.

Given that it uses the database as the source of its generation, it is great for use with existing databases. If you have a new project, the database will need to be created before you can start using Middlegen.

Middlgen is based on JDBC, Velocity, Ant and XDoclet technologies.

I came across Middlegen when investigating the relational persistence tool, Hibernate. I later found out that it has support for generation of the following:

  • Hibernate Persistence Layer
  • EJB (CMP 2.0) Persistence Layer
  • JDO Persistence Layer
  • Torque Persistence Layer
  • Database GUI with using JSP/Struts There is also the option to write Velocity templates and create your own Middlegen plugin.

A listed of databases reported to work with Middlegen is provided on the Middlegen site and used to be be found at: http://boss.bekk.no/boss/middlegen/platforms/database.html

The GUI tool provides a mechanism for changing:

  • Table names
  • Column names
  • Relationships
  • Type mappings Visit the Middlegen website for further details. If you are interested in seeing what Middlegen can generate from your database, return here and try the following quick start guide. Whilst, the steps in the tutorial worked for me, I provide no guarantees they will work for you. I’m certainly no expert in Middlegen, but it is a basic tutorial that should give you some ideas on how to get started.

Quick Start Guide For Middlegen 2.0

  • Download the Middlegen zip distribution.

  • Unzip to your chosen Middlegen home directory (eg, c:\middlegen).

  • Download/install Ant 1.5 or later (setting relevant ANT_HOME, eg C:\apache-ant-1.6.2).

  • Download/install a Java JDK (setting relevant JAVA_HOME, eg C:\Program Files\Java\j2re1.4.2).

  • I don’t believe you need XDoclet, but I did have it installed.

  • Make a copy of the samples directory named something else (eg, c:\middlegen\myapp). (build.xml)

  • Open build.xml and near the top of the file change the following line specifying the database if necessary (note, you will find the options are listed in the “config/database” directory). <!ENTITY database SYSTEM “file:./config/database/db2.xml”>

  • There is also an EJB option below this line that defines your EJB settings. Change this if you are trying to generate the EJB layer. <!ENTITY ejb SYSTEM “file:./config/ejb/jboss.xml”>

  • Just below that, change the following project name line to something more relevant. <project name=” Middlegen Airlines” default=&quotall&quot basedir=&quot.&quot>

  • And below that, change the following property name value line to something more relevant. <property name=“name” value=“airline”/>

  • I removed the Create tables Ant target as it wasn’t necessary. It is used in the example to create a sample table for you to generate from. (<database type>.xml) eg, mysql.xml

  • Remove or ignore the following script file property line which should be the first entry in the file. This is used in the sample to create tables in the database. <property name=“database.script.file”

  • Change properties as required to connect to the database. There are descriptions of the properties in a readme file located in the same directory as the database configuration file you are editing. (build.properties.sample)

  • Remove the “sample” part of this file name. I’m not sure why it is there in the example version, because build.xml references build.properties.

  • Check any of these properties and change them where necessary.

STARTING MIDDLEGEN

Now that the initial configuration is finished, you should be ready to try start Middlegen.

  • Make sure your database is running (please note, you should take all care to backup or take whatever steps you feel are necessary to ensure you can restore your database if something does not go to plan. The creators of this tutorial take no responsibility for errors on your or our part whilst following this guideline).
  • Run the “ant” command from the project configuration directory you have been working in. If you have Ant configured correctly, it should pick up the build.xml file in this directory and run the default command.
  • A number of ant tasks are run, and with any luck you will be provided with the GUI. I believe all the plugins will be loaded. Each one will have a configuration tab. Make changes as you please and to see what Middlegen does, click the “Generate” button.
  • You may need to go back to your console window to see what output messages are being created as Middlegen works its way through the model.
  • When nothing further is output in the console window, Middlegen has probably finished. Go to the “build” directory in your project to find the generated output.
  • After that, it is up to you to decide what to do with it. Good Luck.