论坛首页 Java企业应用论坛

介绍一下关于middlegen自动生成映射文件和ent...

浏览 5684 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-03-12  
介绍一下关于middlegen自动生成映射文件和entity的ant脚本使用说明?

我目前已经可以自动生成了,但是还有些缺陷,就是在生成entity的时候不能指定生成到我想要的包中。

具体说明:

以下为 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.
-->

<!DOCTYPE project [
    <!ENTITY database SYSTEM "file:./db/mysql.xml">
]>


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

<!-- project name="Middlegen Hibernate" default="all" basedir="." -->
 
   <property file="${basedir}/build.properties"/>
   <property name="name" value="CP"/>
   <property name="package" value="mypkg.common.hbm"/>

   <!-- 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="db.dir"                         value="${basedir}/db"/>
   
   <property name="build.dir"                      value="${basedir}/build"/>

   &
   
   <!-- 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>
   </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.
      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 -Dhttp.proxyPort=bar
      
      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>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"
   >
	  <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 then internal then set genIntergratedCompositeKeys to true.
         These settings are optional thus if they are not define here values default to false.
         -->
         <hibernate
            destination="${src.dir}"
            package="[color=red]mypkg.common.hbm[/color]"
            genXDocletTags="false"
            genIntergratedCompositeKeys="true"
         />
      </middlegen>
   </target>

   <!-- =================================================================== -->
   <!-- Compile business logic (hibernate);                                        -->
   <!-- =================================================================== -->
   <target name="compile-hibernate" depends="middlegen" description="Compile hibernate Business Domain Model">
      <javac
         srcdir="${src.dir}"
         destdir="${build.dir}"
         classpathref="lib.class.path"
      >
         <include name="**/hibernate/**/*"/>
      </javac>
   </target>
   
   <!-- =================================================================== -->
   <!-- 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}&password=${database.password}"/>
      <java 
         classname="org.hsqldb.util.DatabaseManager" 
         fork="yes" 
         classpath="${lib.dir}/hsqldb-1.7.1.jar;${database.driver.classpath}"
         failonerror="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 failonerror="no" lenient="no" warn="yes">
      	 <fileset dir="${src.dir}/mypkg/common/hbm" includes="*.xml" />
      </xmlvalidate>
   </target>
   
   <!-- =================================================================== -->
   <!-- Generate ValueObject From Mapping Files                             -->
   <!-- =================================================================== -->
   [color=red]<target name="hbm2java" description="Generate ValueObject From Mapping 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>[/color]
   
</project>



我打算把生成的entity放到mypkg.common.entity的包中而不是和映射文件放在同一个包mypkg.common.hbm中。

请问如何解决啊!!

谢谢!!!
   发表时间:2004-03-14  

<target name="hbm2java...
      <hbm2java            
           output="${src.dir}" 

中的${src.dir}换成你的目标就可以了
0 请登录后投票
   发表时间:2004-03-17  
谢谢楼上的朋友,不过问题依然存在:

我仔细看了一下,如果把

***********************************
&lt;target name="hbm2java...
      &lt;hbm2java           
           output="${src.dir}"

***********************************

中的 output="${src.dir}"
换成output="${src.dir}/mypkg/common/entity"

那么执行ant以后会在${src.dir}/mypkg/common/hbm/mypkg/common/entity/中产生我的两个entity(Customer.java和Product.java)

而且当打开Costomer.java的时候,第一行写着:
package mypkg.common.hbm;

这显然与目录结构不符合,eclipse中显示错误(我在eclipse中执行ant)。

原因是在生成的Customer.hbm.xml和Product.hbm.xml中都有

*********************************
[Customer.hbm.xml]

&lt;class
    name="mypkg.common.hbm.Customer"
    table="customer"
&gt;

[Product.hbm.xml]

&lt;class
    name="mypkg.common.hbm.Product"
    table="product"
&gt;

*********************************

显然问题出现在name属性中。
我觉得应该是name="mypkg.common.entity.Product"
name="mypkg.common.entity.Customer"

才能正确。

请问应该如何解决??

谢谢!!
0 请登录后投票
   发表时间:2004-03-18  
好像hbm.xml和*.java一定会生成在同一目录下,除非手动改,我把他们都放到entity下!
0 请登录后投票
   发表时间:2004-03-18  
哈哈!!

谢谢!!

这就是我的问题所在!!

我今天想出了一个解决方案:


**************************************
&lt;hibernate
            destination=
            "${src.dir}/mypkg/common/hbm"
            package="mypkg.common.entity"
            genXDocletTags="false"
            genIntergratedCompositeKeys="true"
         /&gt;
**************************************
中的package属性设置成未来entity所在的package中,当生成xxx.hbm.xml以后把映射文件再放到期望的目录下。

这样的话不用对源代码进行修改,只是手动对映射文件进行位置的变动。

不过总觉得这种方案有一点不是那么太地道!!!

不知道是否还有什么新方案!!

愿意共同探讨!!

谢谢!!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics