
In previous section,we configured our application to use ant. So far,we have been writing he O/R mapping files (hbm) and writing java code for them manually. Frankly speaking,there is no such need to put so much effort for this kind of stuff as there are different tools available that can be used to generate mapping files from database and then java code from these files. Middlegen is one of these tools,and can be downloaded from http://sourceforge.net/project/showfiles.php?group_id=36044
We will use middlegen in our build script build.xml to generate source from database. Let's add the required libraries to our lib directory.
1. Download and unzip the middlegen distribution along with the dependencies.
2. Copy the jar files from middlegen distribution with dependencies to the lib directory. Also download the hibernate tools distribution from http://hibernate.org/6.html and place the jar file in lib directory. You can find the dependencies and the tools in middlegen/samples/lib directory.
3. Add the following properties in the properties section of the build.xml:
<property name="hbmdir" value="hbm" />
<property name="db.url" value="<database connection url>" />
<property name="db.driver" value="<database driver class>" />
<property name="db.user" value="<database user name>" />
<property name="db.password" value="<database user password>" />
Replace the values of the respective properties with your database JDBC url,Driver name,user name and password. We will use these properties to generate source from source database. The "hbmdir" property will point to the location where we want to place hbm files.
4. Add the following line in "init" target:
<mkdir dir="${hbmdir}"/>
5. Add the following line in "clean" target so that the hbm directory is also deleted with other build directories.
<delete quiet="true" dir="${hbmdir}"/>
6. Add the following target in build.xml:
<target name="gen-hbm" depends="init">
<taskdef name="middlegen" classname="middlegen.MiddlegenTask" classpathref="cp" />
<middlegen appname="hibernate-tutorial" prefsdir="${hbmdir}"
gui="false" databaseurl="${db.url}"
driver="${db.driver}"
username="${db.user}"
password="${db.password}"
schema=""
catalog=""
includeViews="false" >
<table generate="true" name="USERS" />
<table generate="true" name="PHONE_NUMBERS" />
<!-- Plugins -->
<hibernate
destination="${hbmdir}"
package="com.visualbuilder.hibernate"
javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
</middlegen>
<taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="cp" />
<hbm2java output="${build.gen-src.dir}">
<fileset dir="${hbmdir}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
Full contents of this file are given below:
<?xml version="1.0"?> <project name="hibernate-tutorial" default="run" basedir="."> <!-- set global properties for this build --> <property name="srcdir" value="src"/> <property name="jardir" value="jars"/> <property name="builddir" value="build"/> <property name="libdir" value="lib"/> <property name="hbmdir" value="hbm" /> <property name="db.url" value="<database connection url>" /> <property name="db.driver" value="<database driver class>" /> <property name="db.user" value="<database user name>" /> <property name="db.password" value="<database user password>" /> <property name="package" value="com/visualbuilder/hibernate"/> <path id="cp"> <fileset dir="${libdir}"> <include name="*.jar"/> <include name="hibernate/*.jar"/> <include name="hibernate/lib/*.jar"/> </fileset> </path> <target name="init"> <mkdir dir="${builddir}"/> <mkdir dir="${jardir}"/> <mkdir dir="${hbmdir}"/> </target> <target name="clean"> <delete quiet="true" dir="${builddir}"/> <delete quiet="true" dir="${jardir}"/> <delete quiet="true" dir="${hbmdir}"/> </target> <target name="compile" depends="init"> <javac srcdir="${srcdir}" destdir="${builddir}" classpathref="cp"> <include name="${package}/*.java"/> <include name="${package}/client/*.java"/> </javac> </target> <target name="jar" depends="compile"> <jar destfile="${jardir}/app.jar"> <fileset dir="${builddir}"> <include name="${package}/**/*.class"/> </fileset> <fileset dir="${srcdir}"> <include name="${package}/*.hbm.xml"/> <include name="*.cfg.xml"/> </fileset> </jar> </target> <target name="run" depends="jar"> <java classname="com.visualbuilder.hibernate.client.TestClient" classpathref="cp"> <classpath> <pathelement location="${jardir}/app.jar" /> </classpath> </java> </target> <target name="gen-hbm" depends="init"> <taskdef name="middlegen" classname="middlegen.MiddlegenTask" classpathref="cp" /> <middlegen appname="hibernate-tutorial" prefsdir="${hbmdir}" gui="false" databaseurl="${db.url}" driver="${db.driver}" username="${db.user}" password="${db.password}" schema="" catalog="" includeViews="false" > <table generate="true" name="USERS" /> <table generate="true" name="PHONE_NUMBERS" /> <!-- Plugins --> <hibernate destination="${hbmdir}" package="com.visualbuilder.hibernate" javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper" /> </middlegen> <taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="cp" /> <hbm2java output="${hbmdir}"> <fileset dir="${hbmdir}"> <include name="**/*.hbm.xml"/> </fileset> </hbm2java> </target> </project> Middlegen 3 is not available yet,and middlegen 2.1 does not support Hibernate 3. So,to generate java source from hbm using Hibernate Tools,
Replace the database properties values and run the following command on command prompt while residing in the project root directory:
ant gen-hbm
This will generate two hbm files in package folders under the hbm directory.
We have separately generated the hbm and source. If we just replace the hbm and the java files in package com.visualbuilder.hibernate,we will have few conflicts because middlegen has transformed the data types differently. We have used long for the numeric types,but the middlegen actually represents numeric as BigDecimal. So there will be few syntax errors in the client only; on removing which the code should work smoothly.
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin



