Using middlegen to generate hbm and java
Discussion
This article is about how to use middlegen to generate hibernate mapping xml file and POJO.
ant build.xml
<?xml version="1.0"?>
<!DOCTYPE project[
<!ENTITY database SYSTEM "file:./db/mysql.xml">
]>
<project name="bug_management" basedir="." default="all">
<property name="name" value="genkiya" />
<property name="package" value="jp.co.bug_management.common.entity" />
<property name="gui" value="true" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="src.dir" value="${basedir}/src" />
<property name="db.dir" value="${basedir}/db" />
&database;
<path id="lib.class.path">
<pathelement path="${database.driver.classpath}"/>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<!-- ====================== -->
<!-- Task Initilization -->
<!-- ======================-->
<target name="init">
<available
property="xdoclet1.2+"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="lib.class.path"/>
</target>
<!-- ====================== -->
<!-- Fails if XDoclet 1.2.x is not on classpath -->
<!-- ====================== -->
<!--target name="fail-if-no-xdoclet-1.2" unless="xdoclet1.2+">
<fail>
You must download several jar files before you can build Middlegen.
Execute the "download-deps" target. Then try to build again.
If you are behind a proxy, you should define the properties
http.proxyHost and http.proxyPort. Example:
ant -Dhttp.proxyHost=foo.com -Dhttp.proxyPort=8080
It's also possible to download the jars manually.
</fail>
</target-->
<!-- ====================== -->
<!-- Create tables -->
<!-- ====================== -->
<!--target name="create-tables"
depends="init,fail-if-no-xdoclet-1.2,
check-driver-present,panic-if-driver-not-present"
description="Create tables">
<echo>Create tables using URL ${database.url}</echo>
<sql
classpath="${database.driver.classpath}"
driver="${database.driver}"
url="${database.url}"
userid="${database.userid}"
password="${database.password}"
src="${database.script.file}"
print="true"
output="result.txt"/>
</target-->
<target name="check-driver-present">
<available file="${database.driver.file}"
type="file" property="driver.present"/>
</target>
<target name="panic-if-driver-not-present"
unless="driver.present">
<fail>
The JDBC driver you have specified by
including one of the files in ${basedir}/lib
doesn't exist. You have to download this
driver separately and put it in ${database.driver.file}
Please make sure you're using a version
that is equal or superior to the one we looked for.
If you name the driver jar file differently,
please update the database.driver.file property
in the ${basedir}/db/xxx.xml file accordingly.
</fail>
</target>
<!-- =================== -->
<!-- Run Middlegen -->
<!-- =================== -->
<target
name="middlegen"
description="Run Middlegen"
unless="middlegen.skip"
depends="init,check-driver-present,
panic-if-driver-not-present">
<taskdef
name="middlegen"
classname="middlegen.MiddlegenTask"
classpathref="lib.class.path"/>
<middlegen
appname="${name}"
prefsdir="${src.dir}"
gui="${gui}"
databaseurl="${database.url}"
initialContextFactory="${java.naming.factory.initial}"
providerURL="${java.naming.provider.url}"
datasourceJNDIName="${datasource.jndi.name}"
driver="${database.driver}"
username="${database.userid}"
password="${database.password}"
schema="${database.schema}"
catalog="${database.catalog}"
includeViews="false">
<hibernate
destination="${src.dir}/jp/co/bug_management/common/hbm"
package="${package}"
genXDocletTags="true"
javaTypeMapper=
"middlegen.plugins.hibernate.HibernateJavaTypeMapper">
</hibernate>
</middlegen>
</target>
<!-- =================== -->
<!-- Build Everything -->
<!-- =================== -->
<target name="all" depends="middlegen" description="Build Everything"/>
<!-- =================== -->
<!-- Genernate ValueObject From Mapping Files -->
<!-- =================== -->
<target
name="hbm2java"
depends="middlegen"
description="Generate .java from .hbm files.">
<taskdef
name="hbm2java"
classname=
"net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="lib.class.path"/>
<hbm2java
output="${src.dir}"
classpathref="lib.class.path">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
</project>
file:./db/mysql.xml :
<!-- ============================================ -->
<!-- ant properties/targets for mysql -->
<!-- note: this is not a proper xml file (there is no root element) -->
<!-- it is intended to be imported from a *real* xml file -->
<!-- ============================================ -->
<property name="database.script.file"
value="${db.dir}/create_dlfl.sql"/>
<property name="database.driver.file"
value="${lib.dir}/mysql-connector-java-5.0.7-bin.jar"/>
<property name="database.driver.classpath"
value="${database.driver.file}"/>
<property name="database.driver"
value="com.mysql.jdbc.Driver"/>
<property name="database.url"
value="jdbc:mysql://127.0.0.1:3306/bug_management"/>
<property name="database.userid"
value="root"/>
<property name="database.password"
value="mysql"/>
<property name="database.schema"
value="bug_management"/>
<property name="database.catalog"
value=""/>
Required jar files:
commons-collections-3.2.jar |
commons-lang-2.3.jar |
commons-logging-1.1.jar |
commons-pool-1.3.jar |
commons-validator-1.3.1.jar |
hibernate2.jar |
hibernate-tools.jar |
jdom.jar |
log4j-1.2.8.jar |
middlegen-2.1.jar |
middlegen-hibernate-plugin-2.1.jar |
mysql-connector-java-5.0.7-bin.jar |
velocity-1.4-dev.jar |
xdoclet-hibernate-module-1.2.2-RC1.jar |
put all these jar files into /lib directory.
Run ant task [all] or [hbm2java].