Hibernate 3.6 + Hibernate-tools + Ant生成POJO

最近在阅读《Hibernate高手秘籍》一书中,在第一章提到了使用Hibernate 以及Hibernate的扩展工具来生成POJO类,但是那本书是基于Hibernate2.X的。而现在的hibernate版本4.0都已经alpha了。由于我的Hibernate 版本是3.6。 所以,书中的代码根本不能运行成功。

下面我就以自己的一个示例工程来运行下书中的例子.
首先下载项目所依赖的jar包,你可以到官方网站上去下载所需要的jar包,我这里提供了一个。里面含有所有的jar 。包括hibernate-tools里面的freemarker.jar & hibernate-tools.jar
还有一些其他的相关jar包。因为jar包大小超过了10M,附件不支持,所以请需要的到这个URL去下载。

http://download.csdn.net/source/3096976

首先我们新建一个目录结构,如下:

引用

Hibernate
    +---build.xml
    +---src
        +---org
            +--tony
               +--hh   //映射文件存放在hh目录下,同时这个目录存放我们生成的POJO
    +---lib         //这个目录存放我们的jar包
    +---data


目录结构创建好了之后,我们再进行第二步,创建编写我们的build.xml ,内容如下

 <project name="Harnessing Hibernate" basedir=".">
 <property name="source.root" value="src"/>
 <property name="class.root" value="classes"/>
 <property name="lib.dir" value="lib"/>
 <property name="mapping.xml.path" value="src" />
 
 <path id="project.class.path">
 <pathelement location="${class.root}"/>
 <fileset dir="${lib.dir}">
 <include name="*.jar"/>
 </fileset>
 </path>
 
 <target name="hibernatetool">
 <taskdef name="hibernatetool" 
 classname="org.hibernate.tool.ant.HibernateToolTask" 
 classpathref="project.class.path"/>
 
 <hibernatetool destdir="${source.root}">	
 <classpath>
 <path location="${source.root}" />
 </classpath>
 <configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
 <hbm2java/>
 </hibernatetool>
 </target>
 </project>
 



接下来我们将在hh目录下建立一个测试文件User.hbm.xml,内容如下:

 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
 <class name="org.tony.hh.User" table="USER">
 <meta attribute="class-description">
 简单的测试
 @author tony
 </meta>
 
 <id name="id" type="int" >
 <meta attribute="scope-set">private</meta>
 <generator class="native"/>
 </id>
 
 <property name="username" type="string"/>
 <property name="password" type="string"/>
 </class>
 </hibernate-mapping>
 



最后一步,建立在src目录下建立hibernate.cfg.xml配置文件,内容如下:

 <?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <session-factory>
 <property name="connection.username">root</property>
 <property name="connection.password">XXX</property><!--Your DB password here.-->
 <property name="connection.url">jdbc:mysql://localhost:3306/my</property>
 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 <mapping resource="org/tony/hh/User.hbm.xml" />
 </session-factory>
 </hibernate-configuration>
 
 



最后进入build.xml所在目录,执行ant。再进你的源代码目录下,你会看到自动生成的POJO类。

提示:在build.xml中,配置hibernatetool的时候,请你一定要加上classpath属性,否则,有可能会出现如下错误:

[hibernatetool] org.hibernate.MappingNotFoundException: resource: XXX.hbm.xml not found



祝大家好运。





posted @ 2011-03-16 12:43  anylinux  阅读(2189)  评论(0编辑  收藏  举报