Hibernate3中的hbm2java和hbm2ddl的ant脚本实现(转)

来源:互联网 发布:风景调色教程 知乎 编辑:程序博客网 时间:2024/04/25 10:19
最近在看那个很火的《精通Hibernate》,发现里面都是针对hibernate2.1进行讲解了,但我们用的是hibernate3.1,很多地方都不一样,原来的那些hbm2ddl,hbm2java的脚本都不管用了,于是到处查资料,折腾了两天,终于搞定了,希望后来人不要再走弯路。



1.Hibernate-tools.jar这个包已经不像以前那么好找了,现在是放在hibernate-tools这个eclipse插件里面,用的时候要从里面搜索一下才能找到。


2.Hibernate-tools这个插件的用法,见官方网站的flash演示,使用非常方便。
Ant脚本已经跟2有很大差别了,这个我折腾了一天多,终于搞定了,贴出来大家看一下,更详细的信息可以看 http://www.hibernate.org/hib_docs/tools/ant/



Hbm2ddl:

代码<target name="hbm2ddl">
<taskdef name="hbm2ddl"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<hbm2ddl destdir="${ schema.dir }">
<configuration configurationfile="${ source.root }/hibernate.cfg.xml" />
<hbm2ddl export="true" console="false" create="true" update="false" drop="false" outputfilename="bestunix.sql"/>
</hbm2ddl>
</target>



Hbm2java :

代码<target name="hbm2java">
<taskdef name="hbm2java"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<hbm2java destdir="${ source.root }">
<configuration configurationfile="${ source.root }/hibernate.cfg.xml" />
<hbm2java generics="true" ejb3="false"/>
<cfg2hbm/>
</hbm2java>

</target>



同时完成两个任务:

代码<target name="schemaexport">
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<hibernatetool destdir="${ source.root }">
<configuration configurationfile="${ source.root }/hibernate.cfg.xml" />
<hbm2ddl export="true" console="false" create="true" update="false" drop="false" outputfilename="bestunix.sql"/>
<hbm2java generics="true" ejb3="false"/>
<cfg2hbm/>
</hibernatetool>
</target>



3. 用来生成html格式的数据库文档<hbm2doc>在ant脚本中好像暂时不行,还是要借助Hibernate-tool这个Eclipse插件来完成