基于HIBERNATE3环境中,如果想通过ANT来完成HBM文件到DDL或POJO的转换,首先,需要以下库文件:
hibernate-tools.jar
hibernate3.jar
freemarker.jar
mysql-connector-java-5.0.7-bin.jar
antlr-2.7.6.jar
cglib-nodep-2.1_3.jar
commons-collections.jar
commons-logging.jar
dom4j-1.6.1.jar,
接下来,需要准备hibernate.properties文件,内容如下:
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/test
hibernate.connection.username=root
hibernate.connection.password=root
ANT的BUILD文件内容如下:
<project name="testHibernate" default="schemaexport" basedir=".">
<property name="source.root" value="src"/>
<property name="config.dir" value="config"/>
<property name="class.root" value="bin"/>
<property name="lib.dir" value="lib"/>
<path id="project.class.path">
<pathelement path="${classpath}"/>
<pathelement location="${class.root}"/>
<!-- Include jars in the project library directory -->
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="schemaexport">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="project.class.path"/>
<schemaexport
properties="${config.dir}/hibernate.properties"
quiet="no" text="no" drop="no" delimiter=";" output="${config.dir}/schema-export.sql">
<fileset dir="${source.root}">
<include name="mytest/hibernate/firstSample/User.hbm.xml"/>
</fileset>
</schemaexport>
</target>
<target name="schemaupdate">
<taskdef name="schemaupdate"
classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
classpathref="project.class.path"/>
<schemaupdate
properties="${config.dir}/hibernate.properties"
quiet="no">
<fileset dir="src">
<include name="mytest/hibernate/firstSample/User.hbm.xml"/>
</fileset>
</schemaupdate>
</target>
<target name="hbm2java">
<taskdef name="hbm2java"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<hbm2java destdir="${source.root}">
<configuration configurationfile="${config.dir}/hibernate.cfg.xml" />
<hbm2java ejb3="false"/>
</hbm2java>
</target>
</project>
ECLIPSE的PROJECT的文件结构如下: