Skip to content

Middlegen Sample Simple build file

Middlegen is a timesave.
Although it's not the perfect solution to generate Hibernate mapping files and hence POJOs, yet, it saved me a lot of time.

I was about to quit playing with it as it wouldn't read foreign key names with the exception:
java.lang.IllegalArgumentException: There is no column named
<columnname> in the table named <tablename>

It seemed that Middlegen was reading the DB2 AS/400-assigned Foreign Key short
table names (<table>_00001) and not the long names (<long_table_name>).
I doubt the reason is because of discrepancies in DB2/400 (again:I hate DB2/400).

Anyway, after the folks here decided to go with short table names (because of other reasons) everything went fine.

For those of you who want to generate Hibernate mapping files and POJOs from an existing database, here's a SIMPLE middlegen build file (other than the long build sample file supplied with middlegen solution):(Running first middlegen, then hbm2java)

<?xml version="1.0"?>
<project name="my project" basedir="." default="middlegen">

<property  name="middlegen.dir"  value="path/to/middlegen/directory"/>
<property  name="hib.ext.dir"  value="path/to/hibernate/tools/directory"/>
<property  name="" value="application name"/>
<property  name="gen-src.dir" value="${basedir}/JavaSource"/>
<property  name="lib.dir" value="${basedir}/WebContent/WEB-INF/lib"/>
<property  name="src.dir" value="${basedir}/JavaSource"/>
<property  name="" value=""/>

<property name="database.url" value="jdbc:as400://hostname"/>
<property  name="database.driver.file" value="databasedriver.jar" />
<property  name="database.driver.classpath" value="${database.driver.file};${basedir}"/>
<property  name="database.driver" value="" />
<property name="database.userid" value="username"/>
<property name="database.password" value="password"/>
<property name="database.schema" value="library_name"/>
<property name="database.catalog" value="catalog_name"/>

   <!-- =================================================================== -->
   <!-- library                                                             -->
   <!-- =================================================================== -->
   <path id="lib.class.path">
      <pathelement path="${basedir}"/>
      <pathelement path="${database.driver.classpath}"/>
      <pathelement path="${lib.dir}"/>

      <fileset dir="${lib.dir}">
         <include name="**/*.jar"/>

      <fileset dir="${hib.ext.dir}">
         <include name="**/*.jar"/>

      <fileset dir="${middlegen.dir}">
         <include name="*.jar"/>

   <!-- =================================================================== -->
   <!-- Run Middlegen                                                       -->
   <!-- =================================================================== -->
      description="Run Middlegen" 



<!-- Table names are optional -->
<TABLE name="tablename"/>


   <!-- =================================================================== -->
   <!-- Run hbm2java                                                        -->
   <!-- =================================================================== -->
<target name="hbm2java" description="Generate .java from .hbm files.">
<hbm2java  output="${gen-src.dir}">
<fileset dir="${gen-src.dir}/com/ansdk/cmms/domain">
<include name="**/*.hbm.xml"/>
<fileset dir="${basedir}/hibernate.more.hbm">
<include name="**/*.hbm.xml"/>


Now, I'd love to see the following improvements on middlegen;

  • Ability to configure custom id generators. (I now have to run an ant task that replaces the “assigned” id generator with my custom one).
  • Resolving the issue of strange generated property names when having multiple fields referencing the same table as a foreign key (dual foreign keys) (I have to replace them by hand so far)
  • Improvements on the UI when having more than 10 tables (it's like hell when you're having 50+ ones).
  • Any other improvements would be fine (looks like middlegen development is coming to a stop)

Posted in Java.

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Nick Christy says

    Depending on what tools you have available, MyEclipse has a great auto-generator for mapping files. All you need to do is enter a few connection strings to your database and press a button and the files are automatically generated for you. It may take a little bit of cleaning up to customize them the way you wish, but I find this very very helpfull, and doesn’t require any build files.

Some HTML is OK

or, reply to this post via trackback.