Hibernate 3.6 + Hibernate-tools + Ant生成POJO

[size=medium]最近在阅读《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去下载。[/size]
[url]http://download.csdn.net/source/3096976[/url]

[size=medium]首先我们新建一个目录结构,如下:[/size]

[quote]
Hibernate
+---build.xml
+---src
+---org
+--tony
+--hh //映射文件存放在hh目录下,同时这个目录存放我们生成的POJO
+---lib //这个目录存放我们的jar包
+---data
[/quote]
[size=medium]目录结构创建好了之后,我们再进行第二步,创建编写我们的build.xml ,内容如下[/size]

<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>


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

<?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>


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

<?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>



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

[size=medium][color=red]提示:在build.xml中,配置hibernatetool的时候,请你一定要加上classpath属性,否则,有可能会出现如下错误:[/color][/size]
[hibernatetool] org.hibernate.MappingNotFoundException: resource: XXX.hbm.xml not found


祝大家好运。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值