使用XDoclet生成hbm.xml

来源:互联网 发布:php收银系统源码 编辑:程序博客网 时间:2024/06/12 01:14

 

使用XDoclet生成hbm.xml- -

                              

Google一下网上用XDoclet生成Hibernate Mapping和建表的文章很多,我以前都是先把表建好,然后手工写Mapping文件,这样很费时间,用XDoclet后在建模过程中在POJO类中加入XDoclet标签,这样节省了很多时间。
用XDoclet生成hbm.xml就是在.java文件里写入一些元数据,XDoclet会从这些数据以及类本身得到足够的信息来生成目标文件。当然,除了用于hibernate,XDoclet还可以用于web、ejb等等很多用途。
  XDoclet要从sourceforge上下载,包含了很多jar包、文档和例子,我觉得文档做得还是不错的,查起来比较方便。要使用XDoclet,一般要通过ant来完成,也就是在ant脚本里加入XDoclet的内容。
  由于eclipse已经包含了ant支持,因此我没有专门去下载一个ant回来,而是直接使用eclipse带的,版本是1.5.3。
  建立ANT的Build文件,输入代码如下:
<project name="Hibernate Example" default="about" basedir="../../">
 <!-- The location where your xdoclet jar files reside -->
 <!--xdoclet-1.2b3-dev built from CVS"/-->
 <property name="xdoclet.lib.home" value="c:/lib/xdoclet-1.2.2/lib"/>
 <property name="properties.dir"   value="${basedir}/ssh/WEB-INF/classes/" />
 <property name="hibernate.lib.home" value="C:/lib/hibernate-2.1.7/lib"/>
 <property name="db2.lib.home" value="d:/"/>
 <property name="pojo.dir" value="${basedir}" />
 
 <target name="clean" depends="init" description="removes all directories related to this build">
  <delete dir="${dist}"/>
 </target>
 
 <target name="init" description="Initializes properties that are used by other targets.">
  <property name="dist" value="dist"/>
 </target>
 
 <target name="prepare" depends="init,clean" description="creates dist directory">
  <echo message="Creating required directories..."/>
  <mkdir dir="${dist}"/>
 </target>
 
 <target name="hibernate" depends="prepare"
        description="Generates Hibernate class descriptor files.">               
  <taskdef name="hibernatedoclet"
      classname="xdoclet.modules.hibernate.HibernateDocletTask">     
      <classpath>
   <fileset dir="${xdoclet.lib.home}">
       <include name="*.jar"/>
   </fileset>
      </classpath>     
  </taskdef>

  <!-- Execute the hibernatedoclet task -->
  <hibernatedoclet
   destdir="${basedir}"
   excludedtags="@version,@author,@todo"
   force="true"
   verbose="true">
  
   <fileset dir="${basedir}">
       <include name="**/ssh/pojo/*.java"/>
   </fileset>

   <hibernate version="2.0"/>

  </hibernatedoclet>     
 </target>
 
<!-- ================================================================== -->
<!-- Export Database Schema from mapping files                          -->
<!-- ================================================================== -->

 <target name="db-schema">
      <path id="hibernate.mapping.files" >
            <fileset dir="${basedir}">
                 <include name="**/ssh/pojo/*.hbm.xml" />
             </fileset>
      </path>
     <pathconvert refid="hibernate.mapping.files" property="hibernate.mappings" pathsep=" "/>
     <java classname="net.sf.hibernate.tool.hbm2ddl.SchemaExport" fork="true">
         <!-- mapping file -->
         <arg line="${hibernate.mappings} --text --format --output=example_schema.ddl --delimiter=;"/>
         <classpath>
             <pathelement location="${properties.dir}"/>
             <fileset dir="${hibernate.lib.home}">
                 <include name="*.jar" />
             </fileset>
            
            <fileset dir="${db2.lib.home}">
                 <include name="**/*.jar" />
                 <include name="**/*.zip" />
             </fileset>
            
             <!-- build output path -->
             <pathelement location="."/>
         </classpath>
    </java>
 </target>
 
 <target name="about" description="about this build file" depends="init">
  <echo message="  Use this format for the arguments:"/>
  <echo message="      ant hibernate"/>
  <echo message="      ant db-schema"/>
  <echo message=""/>  
 </target>
</project>

很简单,但是最容易出问题的地方就路径关系没有理顺好。注意<project name="Hibernate Example" default="about" basedir="../../"> 中的basedir,之后所有的路径都以它为基础。

原创粉丝点击