服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

源代码分享――进化中hibernate3脚本


  在hibernate有一些相当方便的辅助工具: hbm2java,hbm2ddl, 数据库的逆向工程,mapping editor.
  
  这些任务可以通过ant构建完成,hibernate提供了ant tasks及其构建脚本.由于hibernate从2到3进行了重大重构,且包重新做了组织,因此ant构建脚本也发生了巨大变化.在2中脚本样式为:
  
  <?xml version="1.0"?>
  
  <project name="anto builder"
  
  default="db" basedir=".">
  
  <!-- set up properties containing important project directories -->
  
  <property name="source.root" value="src"/>
  
  <property name="class.root" value="classes"/>
  
  <property name="lib.dir" value="lib"/>
  
  <property name="data.dir" value="data"/>
  
  <!-- set up the class path for compilation and execution -->
  
  <path id="project.class.path">
  
  <!-- include our own classes, of course -->
  
  <pathelement location="${class.root}" />
  
  <!-- include jars in the project library directory -->
  
  <fileset dir="${lib.dir}">
  
  <include name="*.jar"/>
  
  </fileset>
  
  </path>
  
  <target name="db" description="runs hsqldb database management uiagainst the database file--use when application is not running">
  
  <java classname="org.hsqldb.util.databasemanager"
  
  fork="yes">
  
  <classpath refid="project.class.path"/>
  
  <arg value="-driver"/>
  
  <arg value="org.hsqldb.jdbcdriver"/>
  
  <arg value="-url"/>
  
  <arg value="jdbc:hsqldb:${data.dir}/music"/>
  
  <arg value="-user"/>
  
  <arg value="sa"/>
  
  </java>
  
  </target>
  
  <!-- teach ant how to use hibernate's code generation tool -->
  
  <taskdef name="hbm2java"
  
  classname="net.sf.hibernate.tool.hbm2java.hbm2javatask"
  
  classpathref="project.class.path"/>
  
  <!-- generate the java code for all mapping files in our source tree -->
  
  <target name="codegen"
  
  description="generate java source from the o/r mapping files">
  
  <hbm2java output="${source.root}">
  
  <fileset dir="${source.root}">
  
  <include name="**/*.hbm.xml"/>
  
  </fileset>
  
  </hbm2java>
  
  </target>
  
  <!-- create our runtime subdirectories and copy resources into them -->
  
  <target name="prepare" description="sets up build structures">
  
  <mkdir dir="${class.root}"/>
  
  <!-- copy our property files and o/r mappings for use at runtime -->
  
  <copy todir="${class.root}" >
  
  <fileset dir="${source.root}" >
  
  <include name="**/*.properties"/>
  
  <include name="**/*.hbm.xml"/>
  
  </fileset>
  
  </copy>
  
  </target>
  
  <!-- compile the java source of the project -->
  
  <target name="compile" depends="prepare"
  
  description="compiles all java classes">
  
  <javac srcdir="${source.root}"
  
  destdir="${class.root}"
  
  debug="on"
  
  optimize="off"
  
  deprecation="on">
  
  <classpath refid="project.class.path"/>
  
  </javac>
  
  </target>
  
  <!-- generate the schemas for all mapping files in our class tree -->
  
  <target name="schema" depends="compile"
  
  description="generate db schema from the o/r mapping files">
  
  <!-- teach ant how to use hibernate's schema generation tool -->
  
  <taskdef name="schemaexport"
  
  classname="net.sf.hibernate.tool.hbm2ddl.schemaexporttask"
  
  classpathref="project.class.path"/>
  
  <schemaexport properties="${class.root}/hibernate.properties"
  
  quiet="no" text="no" drop="no">
  
  <fileset dir="${class.root}">
  
  <include name="**/*.hbm.xml"/>
  
  </fileset>
  
  </schemaexport>
  
  </target>
  
  </project>
  
  在3中,构建脚本为:
  
  <?xml version="1.0" encoding="gbk"?>
  
  <project name="hibernate" default="anthbm2java" basedir=".">
  
  <property name="src.dir" value="./src" />
  
  <property name="class.dir" value="./bin" />
  
  <property name="lib.dir" value="d:/eclipse/plugins/org.hibernate.eclipse_3.0.0.alpha4/lib" />
  
  <path id="project.class.path">
  
  <fileset dir="${lib.dir}">
  
  <include name="*.jar"/>
  
  </fileset>
  
  <pathelement location="${class.dir}" />
  
  </path>
  
  <path id="tasks.classpath">
  
  <fileset dir="${lib.dir}">
  
  <include name="*.jar"/>
  
  </fileset>
  
  <pathelement location="${class.dir}" />
  
  </path>
  
  <target name="anthbm2java">
  
  <taskdef name="hibernatetool"
  
  classname="org.hibernate.tool.ant.hibernatetooltask"
  
  classpathref="tasks.classpath"/>
  
  <hibernatetool destdir="${class.dir}">
  
  <configuration configurationfile="hibernate.cfg.xml" >
  
  <fileset dir="${src.dir}">
  
  <include name="**/*.hbm.xml" />
  
  </fileset>
  
  </configuration>
  
  <hbm2ddl export="false" console="false" create="true" update="false" drop="false" outputfilename="broad.sql" delimeter=";"/>
  
  其中的delimeter属性在hibernate-tool 3 a5版本中不支持.
  
  <hbm2java generics="true" ejb3="false"/>
  
  <cfg2hbm/>
  
  <hbm2doc/>
  
  <!--
  
  <cfg2cfgxml/>
  
  这个任务在hibernate-tool 3 a5版本中不支持.  -->
  
  </hibernatetool>
  
  </target>
  
  </project> hibernate3构建脚本的变化
  
  这个脚本在eclipse中检验过.
  
  通过这个脚本,执行了很多hibernate辅助工具的功能,方便了开发.

扫描关注微信公众号