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
in the table named ` It seemed that Middlegen was reading the DB2 AS/400-assigned Foreign Key short table names (_00001) and not the long names (). 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) {% highlight xml %} <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=”app.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=”domain.package.name” value=”com.package.name”/> <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=”com.ibm.as400.access.AS400JDBCDriver” /> <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> <fileset dir=”${hib.ext.dir}”> <include name=”**/*.jar”/> </fileset> <fileset dir=”${middlegen.dir}”> <include name=”*.jar”/> </fileset> </path> <!– =================================================================== –> <!– Run Middlegen –> <!– =================================================================== –> <target name=”middlegen” description=”Run Middlegen” > <taskdef name=”middlegen” classname=”middlegen.MiddlegenTask” classpathref=”lib.class.path” /> <middlegen appname=”${app.name}” prefsdir=”${src.dir}” gui=”false” includeViews=”false” databaseurl=”${database.url}” driver=”${database.driver}” username=”${database.userid}” password=”${database.password}” schema=”${database.schema}” catalog=”${database.catalog}” > <!– Table names are optional –> <!– <TABLE name=”tablename”/> –> <hibernate destination=”${gen-src.dir}” standardGeneratorScheme=”assigned” package=”${domain.package.name}” genXDocletTags=”true” javaTypeMapper=”middlegen.plugins.hibernate.HibernateJavaTypeMapper” /> </middlegen> </target> <!– =================================================================== –> <!– Run hbm2java –> <!– =================================================================== –> <target name=”hbm2java” description=”Generate .java from .hbm files.”> <taskdef name=”hbm2java” classname=”net.sf.hibernate.tool.hbm2java.Hbm2JavaTask” classpathref=”lib.class.path” /> <hbm2java output=”${gen-src.dir}”> <fileset dir=”${gen-src.dir}/com/ansdk/cmms/domain”> <include name=”**/*.hbm.xml”/> </fileset> <fileset dir=”${basedir}/hibernate.more.hbm”> <include name=”**/*.hbm.xml”/> </fileset> </hbm2java> </target> </project> {% endhighlight %} 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)