使用hbm2Java生成代码是非常轻易的,比hibernate提供的schemaEXPort(配置文件到数据库)的使用轻易得多。真不知道作者怎么搞的,把个schemaExport写得这么难...... 1.开始前的预备 1.1 预备hibernate-2.1.6.zip 1.2 预备hibernate-extensions-2.1.2.zip 1.3 预备logging-log4j-1.2.9.zip 1.4 把相关.jar文件全部复制到工程目录之下的lib目录 1.5 知道如何使用ant。可阅读我写的文章"非常简单的ant文件"2.创建工程目录及文件结构如下
Payment <dir>-src <dir>-hbm <dir> -payment <dir> -Payment.hbm.XML-classes <dir>-lib <dir>-build.XML-hibernate.codegen.XML-log4j.properties3.各文件代码 3.1 build.XML <project name="" basedir="./" default="g"> <path id="p"> <fileset dir="lib/"> <include name="**/*.jar"/> </fileset> </path> <target name="g" > <taskdef name="g" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref=../../"p"/> <g config="hibernate.codegen.XML" output="src"> <fileset dir="hbm"> <include name="**/*.hbm.XML"/> </fileset> </g> </target></project> 3.2 hibernate.codegen.XML <?XML version='1.0' encoding='gbk'?><codegen> <generate renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer"/></codegen> 3.3 log4j.properties # Configure logging for testinglog4j.rootLogger=DEBUG, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.SimpleLayout 3.4 Payment.hbm.XML<?XML version="1.0" encoding="gbk"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="payment.Payment" table="PAYMENT"> <id name="id" type="long" column="PAYMENT_ID"> <generator class="native"/> </id> <property name="amount" column="AMOUNT" type="long"/> </class></hibernate-mapping>4.说明 4.1 build.XML文件中将hibernate extensions包提供的net.sf.hibernate.tool.hbm2java.Hbm2JavaTask定义为一个任务。