Using Middlegen to create the *.hbm and POJO Classes

Using Middlegen to create the *.hbm and POJO Classes

By https://betterpetshop.dev.java.net

 

This documentation describes how to create the POJO from the *.hbm files created by middlegen.

 

 

Requirements

 

Ant 1.5 +

Xdoclet 1.2+

JDK 1.4 +

Hibernate 2.1.2

Hibernate-extensions-2.1.2

Middlegen-Hibernate-r4

JDBC Driver for your database

 

 

Direction

 

Browse to Middlegen-Hibernate-r4 installation directory and find the build.xml.

Modify the build.xml to the following:

(Please note comments starting with John Mammen (in Red)  is where I made changes, and most probably you would need to change it also)

 

File: build.xml

 

<?xml version="1.0"?>

 

<!--

This is a testing platform for the middlegen hibernate plugin. It is a stripped

down version of the samples application. At the moment it is only concerned

with the generation of the mapping documents so that various tests etc can be

done on the generated package.

 

   David Channon

 

 

The DOCTYPE declaration declares the location of product-specific parts of the

Ant build file. You can change the values of these declarations to point to

other available files. This gives you an opportunity to choose what database.

 

Note that this syntax has nothing to do with Ant or Middlegen. This

is the standard way defined by w3c to dynamically include external

files in an XML document. Any decent XML parser will do the include

transparently. Try to open this file in Internet Explorer and see

for yourself.

-->

 

 

<!— by John Mammen

THIS POINTS TO THE DATABASE DETAILS

    MODIFY THE REFERENCED FILE FOR YOUR DATABASE

-->

   

<!DOCTYPE project [

    <!ENTITY database SYSTEM "file:./config/database/mysql.xml">

]>

 

 

<project name="Middlegen Hibernate" default="all" basedir=".">

 

<!-- project name="Middlegen Hibernate" default="all" basedir="." -->

 

   <property file="${basedir}/build.properties"/>

 

 

 

<!— by John Mammen

    The below is project name, change this as

    this will be used in package names

-->

   <property name="name" value="sample"/>

   <!-- This was added because we were several people (in a course) deploying to same app server>

   <property environment="env"/>

   <property name="unique.name"                    value="${name}.${env.COMPUTERNAME}"/-->

 

   <property name="gui"                            value="true"/>

 

   <property name="unique.name"                    value="${name}"/>

 

   <property name="appxml.src.file"                value="${basedir}/src/application.xml"/>

   <property name="lib.dir"                        value="${basedir}/lib"/>

   <property name="src.dir"                        value="${basedir}/src"/>

   <property name="java.src.dir"                   value="${src.dir}/java"/>

   <property name="web.src.dir"                    value="${src.dir}/web"/>

   <property name="jdbc.lib.dir"                   value="D:/msjdbclib"/>

  

   <property name="build.dir"                      value="${basedir}/build"/>

   <property name="build.java.dir"                 value="${build.dir}/java"/>

   <property name="build.gen-src.dir"              value="${build.dir}/gen-src"/>

   <property name="build.classes.dir"              value="${build.dir}/classes"/>

 

   &database;

  

   <!-- define the datasource.jndi.name in case the imported ejb file doesn't -->

   <property name="datasource.jndi.name"           value="${name}/datasource"/>

 

   <path id="lib.class.path">

      <pathelement path="${database.driver.classpath}"/>

      <fileset dir="${lib.dir}">

         <include name="*.jar"/>

      </fileset>

     <!-- jdbc libs

      <fileset dir="${jdbc.lib.dir}">

         <include name="*.jar"/>

      </fileset>-->

      <!-- The middlegen jars -->

      <!--fileset dir="${basedir}/.."-->

      <fileset dir="${basedir}/middlegen-lib">

         <include name="*.jar"/>

      </fileset>

   </path>

 

   <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.

      </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>Creating 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}/config/database

      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}/config/database/xxx.xml file accordingly.

      </fail>

   </target>

 

   <!-- =================================================================== -->

   <!-- Run Middlegen                                                       -->

   <!-- =================================================================== -->

   <target

      name="middlegen"

      description="Run Middlegen"

      unless="middlegen.skip"

      depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"

   >

      <mkdir dir="${build.gen-src.dir}"/>

 

      <echo message="Class path = ${basedir}"/>

      <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}"

      >

 

         <!--

         We can specify what tables we want Data generated for.

         If none are specified, Data will be generated for all tables.

         Comment out the <table> elements if you want to generate for all tables.

         Also note that table names are CASE SENSITIVE for certain databases,

         so on e.g. Oracle you should specify table names in upper case.

         -->

         <!--table generate="true" name="flights" pktable="flights_pk"/>

         <table name="reservations"/-->

   

 

         <!--

         If you want m:n relations, they must be specified like this.

         Note that tables declare in multiple locations must all have

         the same value of the generate attribute.

         -->

         <!--many2many>

            <tablea generate="true" name="persons"/>

            <jointable name="reservations" generate="false"/>

            <tableb generate="true" name="flights"/>

         </many2many-->

 

 

         <!-- Plugins - Only Hibernate Plugin has been included with this special distribution  -->

        

         <!--

         If you want to generate XDoclet markup for hbm2java to include in the POJOs then

         set genXDocletTags to true. Also, composite keys are generated as external classes which is

         recommended. If you wish to keep them internal then set genIntergratedCompositeKeys to true.

         Since r4 the ability to customise the selection of JavaTypes is now provided. The is a

         recommended type mapper provided as shown. It is optional - if not provided then Middlegen

         itself will select the Java mapping (as it did previously).

         These settings are optional thus if they are not define here values default to false.

         -->

         <hibernate

            destination="${build.gen-src.dir}"

            package="${name}.hibernate"

            genXDocletTags="true"

            genIntergratedCompositeKeys="false"

            javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"

         />

        

      </middlegen>

 

      <mkdir dir="${build.classes.dir}"/>

   </target>

 

   <!-- =================================================================== -->

   <!-- Compile business logic (hibernate)                                        -->

   <!-- =================================================================== -->

   <target name="compile-hibernate" depends="middlegen" description="Compile hibernate Business Domain Model">

      <javac

         srcdir="${build.gen-src.dir}"

         destdir="${build.classes.dir}"

         classpathref="lib.class.path"

      >

         <include name="**/hibernate/**/*"/>

      </javac>

   </target>

 

 

<!— by John Mammen

Below is the new target for HBMTOJAVA which
converts all *.hbm files to java classes

-->  

<target name="hbm2java" description="Generate .java from .hbm files."                                   depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present">

 

<taskdef

        name="hbm2java"

            classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"

            classpathref="lib.class.path"

/>

 

<!— by John Mammen

The file (hbm2java.xml) which configures the hbm2java task. I spent one day trying to find this file, then I had to create this.

-->  

<hbm2java config="${src.dir}/hbm2java.xml" output="${build.gen-src.dir}" classpathref="lib.class.path" >

<fileset dir="${build.gen-src.dir}">

            <include name="**/*.hbm.xml"/>

        </fileset>

       </hbm2java>

</target>

<!-- END -->  

 

  

   <!-- =================================================================== -->

   <!-- Build everything                                                    -->

   <!-- =================================================================== -->

   <target name="all" description="Build everything" depends="compile-hibernate"/>

 

   <!-- =================================================================== -->

   <!-- Clean everything                                                    -->

   <!-- =================================================================== -->

   <target name="clean" description="Clean all generated stuff">

      <delete dir="${build.dir}"/>

   </target>

 

   <!-- =================================================================== -->

   <!-- Brings up the hsqldb admin tool                                     -->

   <!-- =================================================================== -->

   <target name="hsqldb-gui" description="Brings up the hsqldb admin tool">

      <property name="database.urlparams" value="?user=${database.userid}&amp;password=${database.password}"/>

      <java

         classname="org.hsqldb.util.DatabaseManager"

         fork="yes"

         classpath="${lib.dir}/hsqldb-1.7.1.jar;${database.driver.classpath}"

         failοnerrοr="true"

      >

         <arg value="-url"/>

         <arg value="${database.url}${database.urlparams}"/>

         <arg value="-driver"/>

         <arg value="${database.driver}"/>

      </java>

   </target>

 

   <!-- ==================================================================== -->

   <!-- Validate the generated xml mapping documents                         -->

   <!-- ==================================================================== -->

   <target name="validate">

      <xmlvalidate failοnerrοr="no" lenient="no" warn="yes">

         <fileset dir="${build.gen-src.dir}/sample/hibernate" includes="*.xml" />

      </xmlvalidate>

   </target>

  

</project>

 

 

After modifying the build.xml, create the following configuration file (hbm2java.xml) for hbm2java task and save it under  src directory.

 

File: hbm2java.xml

 

<codegen>

    <generate renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer"/>

</codegen>

 

 


Now, the database config file can be changed to your specific database. The properties are self-explanotary. Make sure you have the jdbc drivers under the lib directory.

 

File 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="${src.dir}/sql/${name}-mysql.sql"/>

<property name="database.driver.file"           value="${lib.dir}/mysql-connector-java-3.0.0-beta-bin.jar"/>

<property name="database.driver.classpath"      value="${database.driver.file}"/>

<property name="database.driver"                value="org.gjt.mm.mysql.Driver"/>

<property name="database.url"                   value="jdbc:mysql://localhost/airline"/>

   <property name="database.userid"                value=""/>

   <property name="database.password"              value=""/>

   <property name="database.schema"                value=""/>

   <property name="database.catalog"               value=""/>

   <property name="jboss.datasource.mapping"       value="mySQL"/>

 

 

Now you are ready to run the middlegen to create the *.hbm files and POJO classes.

This is a listing of jars in the lib directory, make sure you have them. If you get any class not found errors, please include all of them.

 

bsh-2.0b1.jar                     

commons-collections-2.1.jar

commons-lang-1.0.1.jar            

commons-logging-1.0.3.jar

commons-logging.jar               

commons-pool-1.1.jar

forms-1.0.3.jar

                   

hibernate-tools.jar

hsqldb-1.7.1.jar                  

mysql-jdbc.jar

                    

jdom.jar

jdom.license.txt                  

jgraph.jar

log4j-1.2.7.jar                   

looks-1.1.3.jar

 

pf-joi-full.jar

velocity-1.3.1.jar                

velocity-1.4-dev.jar

xdoclet-1.2.jar                   

xdoclet-hibernate-module-1.2.jar

xdoclet-xdoclet-module-1.2.jar    

xjavadoc-1.0.2.jar

 

 

 

First create the *.hbm file by running ant with no parameters. It should popup with the Middlegen interface, modify the cardinality and directions and click generate. It should create *.hbm files under build/gen-src.

 

Once the *.hbm files are created, run ant with hbm2java. It should create all the POJO files.

 

If you have any problems, please check all the dependent installation and then contact me at https://betterpetshop.dev.java.net

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值