hibernatetools通过hbm.xml生成java POJO源文件和数据库DDL脚本



hibernatetools通过hbm.xml生成java POJO源文件和数据库DDL脚本。hbm2java跟hbm2ddl这两个工具已经跟2的时候完全不一样了,已经全部集成到hibernatetools里了。这个东西的下载已经从hibernate官网链到了https://www.jboss.org/tools/download/stable.html
。我下的是最新的那个3.1GA。这东西是个压缩包,解压到eclipse目录中,就可以作为eclipse的插件来使用。不过要使用hbm2java和
hbm2ddl直接从hbm.xml生成java源文件和ddl的sql脚本,好像在eclipse里还不行,还是得配置ant,使用ant来做这事。要
使用ant来使用那两个java工具,多少也有点麻烦,得自己写build.xml生成配置文件。在这之前还有一点准备工作需要做。我是没跟项目混在一
起,是把hbm.xml文件单拎出来做的测试。大致准备工作如下:
在测试目录中建个lib目录,然后把hibernatetools压缩包中的
plugins\org.hibernate.eclipse_XXXXXXX\lib\tools中的hibernate-tools.jar和
freemarker.jar文件考到lib中,然后还有plugins\org.hibernate.eclipse_XXXXXXX\lib
\hibernate目录中的dom4j,commons-logging,hibernate3,slf4j和log4j也考到lib目录中(我的测试
就需要了这些文件),如果保险和省事的做法,就把lib目录下的所有jar包都考过去(带子目录也没关系,在build.xml中可以很容易的配)。这样
需要的jar包就齐了。
另外如果生成ddl脚本的话,还需要hibernate.properties,log4j也需要它自己的properties文件,log4j.properties,如果没有log4j.properties文件的话,也能生成成功,但会报如下的警告:
log4j:WARN
No appenders could be found for logger (org.apache.catalina.startup.TldConfig).

log4j:WARN Please initialize the log4j system properly.

所以还是加上log4j.properties文件吧。另外注意,这两个properties配置文件不要跟jar包放在同一个目录下,在此例中是lib
目录,如果放在同一目录中,好像就会找不到log4j.properties文件,即使设置了classpath好像也不行,反正我测的就是会出问题。所
以在测试目录中再随便新建一个任一目录,把这两个文件放在里面即可。hibernate.properties用它的xml格式的也可以。
然后在测试目录中创建一个Customber.hbm.xml,如下:
<?xml
version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd”
>
<!–meta元素如果声明在class前则此文件中所有的类都会使用此meta元素的设置,如果只想紧挨着meta的class元素使用此
meta设置,则可以设置meta的inherit为false,最好的还是把meta放到相应的class元素中去。–>
<hibernate-mapping>
<class
table=”customer” name=”hbtest.Customer”>
<meta
attribute=”class-description”>this is a hbm2java test
class</meta>
<meta
attribute=”scope-class”>private</meta><meta
attribute=”scope-field”>protected</meta>

<id name=”id”
column=”ID” type=”int”>
<meta attribute=”scope-get”>protected
static</meta>
<meta
attribute=”scope-set”>protected</meta>
<generator
class=”increment”></generator>
</id>
<property
name=”name” type=”string”>
<meta
attribute=”scope-field”>public</meta>
<meta
attribute=”use-in-tostring”>true</meta>
<column name=”NAME”
not-null=”true” unique=”true”
length=”15″></column></property>
<property name=”age”
type=”int”>
<column name=”AGE” check=”AGE&gt;10″
not-null=”true”></column></property><property name=”married”
column=”ISMARRIED” type=”boolean”></property>
<property
name=”pic” column=”PIC” type=”binary”></property>
<property
name=”birthday” column=”BIRTHDAY” type=”date”>
<meta
attribute=”scope-field”>public</meta></property>
<property
name=”addTime” type=”timestamp”>
<column name=”ADDTIME”
index=”IDX_ADDTIME”
sql-type=”datetime”></column></property>

</class>
</hibernate-mapping>

最后在测试目录中创建build.xml(用别的名也行,在执行ant的时候只要加上-buildfile参数指定配置文件就行)文件:
<?xml
version=”1.0″ encoding=”utf-8″?>
<project name=”hibernate tools test”
default=”prepare” basedir=”.”>
<path id=”class.path”
cache=”true”>
<pathelement location=”abc”/>
<fileset
dir=”lib”>
<include name=”*.jar”
/>
</fileset>
</path>
<target
name=”prepare”>
<delete dir=”test” />
<mkdir dir=”test”
/>
</target>
<target name=”codegen”
depends=”prepare”>
<taskdef name=”hbm2java”
classname=”org.hibernate.tool.ant.HibernateToolTask”
classpathref=”class.path”/>
<hbm2java
destdir=”test”>
<!–此处的hibernate的配置文件不是必须的,没有也不影响最后生成java源文件–>
<configuration
propertyfile=”abc/hibernate.properties”>
<fileset
dir=”.”>
<include
name=”*.hbm.xml”/>
</fileset>
</configuration>
<hbm2java
jdk5=”true”/>
</hbm2java>
</target>
<target
name=”ddlgen” depends=”codegen”>
<taskdef name=”hbm2ddl”
classname=”org.hibernate.tool.ant.HibernateToolTask”
classpathref=”class.path”/>
<hbm2ddl
destdir=”test”>
<!–必须配上hibernate的配置文件,不然会报错,无法生成ddl脚本–>
<configuration
propertyfile=”abc/hibernate.properties”>
<fileset
dir=”.”>
<include
name=”*.hbm.xml”/>
</fileset>
</configuration>
<hbm2ddl
export=”false” outputfilename=”hbm2ddltest.sql”
delimiter=”;”/>
</hbm2ddl>
</target>
</project>
然后在测试目录中执行ant
-buildfile testbuild.xml
ddlgen(这里的ddlgen是最后一个target),因为testbuild.xml中设置的默认target是prepare,所以得在这里设
置运行ddlgen,也可以直接在testbuild.xml文件中的project节点的default设置成ddlgen(这样就不需要在ant命令
中添加ddlgen这个target了),然后根据依赖性,就会自动执行另外的两个target了。最后去查询生成的test目录,就会看到生成的
java源文件和相应的数据库建表sql脚本了。

虽然有点麻烦,但配好了的话,以后只需要写hbm.xml文件即可了,其他的都可以很快的自动生成了。也还算方便吧。凑合!

另外关于hibernatetools的使用方法更详细的说明,以及其ant方面的应用更详细的说明,请参见如下的网址:
http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html/setup.html的ant部分和
http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html/ant.html