Hibernate Tools以Ant Task方式运行,主要在Build.xml中配置。内容如下:
<?xml version="1.0" encoding="UTF-8"?> <project name="University_FG" default="all" basedir="."> <!-- "." represents the src folder --> <!-- Declare path variables to refer the third party libraries required--> <path id="toolslib"> <path location="./lib/hibernate-tools.jar" /> <path location="./lib/hibernate3.jar" /> <path location="./lib/freemarker.jar" /> <path location="./lib/mysql.jar" /> <path location="./lib/mysql-connector-java-5.1.7.jar" /> <path location="./lib/dom4j-1.6.1.jar" /> <path location="./lib/log4j-1.2.15.jar" /> <path location="./lib/slf4j-api-1.5.6.jar" /> <path location="./lib/slf4j-jdk14-1.5.6.jar" /> <path location="./lib/commons-logging-1.1.jar" /> </path> <!-- To include the src and lib names in the path --> <path id="mypath"> <fileset dir="./lib"> <include name="**.*" /> </fileset> </path> <!-- Declare the HibernateToolTask --> <target name="all"> <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="mypath" /> <!-- Declare the destination directory --> <hibernatetool destdir="./src"> <!-- Declare Classpath --> <classpath> <path location="./config" /> </classpath> <!-- Declare the configuration file path --> <configuration configurationfile="./config/hibernate.cfg.xml"> </configuration> <!-- Convert the mapping file to Java source files --> <hbm2java/> <!-- Convert the mapping file to DDL files --> <hbm2ddl export="true" outputfilename="university_sql.ddl" /> </hibernatetool> </target> </project>
build.xml的内容有两部分:
第一部分是Path,主要是引入所需要的Jar包
第二部分是Hibernate Tools的相关配置。
属性名称 | 定义 | 使用情况 |
destdir | 生成文件的输入目录 | Required |
templatepath | 用户编辑模板的路径 | Optional |
classpath | 解析资源时的依赖环境 | Optional,但通常是要求 |
property(and propertyset) | 控制输出的属性设置,大部分与用户定义模板提供属性有关。 | Optional |
configuration (annotationconfiguration, jpaconfiguration, jdbcconfiguration) | 必须为Hibernate元模型指定四个属性之一。 | Required |
hbm2java(hbm2cfgxml, hbmtemplate, etc.) | 指定一个或多个的输出方式 | Required |
本例中就有两种输出方式:
一种是根据Mapping生成POJO文件,一种是根据Mapping生成DDL,并生成数据库表。