在数据库建立后,可以用Middlegen生成每个表对应的hbm.xml文件,并用hibernate-extensions生成对应的Java类。在后期开发时,如果要修改表,修改完后再走一遍这样的流程,那就显得有些繁琐。用XDoclet可以直接根据修改后的Java类生成对应的hbm.xml文件,这样可以减少维护的工作量。下面是我的一个简单例子。
我使用的数据库是MySQL 4.1.11,在数据库test中有这样一个用户表:
1
CREATE TABLE IF NOT EXISTS User
2
(
3
Guid INT NOT NULL AUTO_INCREMENT,
4
Account VARCHAR(64) NOT NULL,
5
Password VARCHAR(16) NOT NULL,
6
Email VARCHAR(128) NOT NULL,
7
PRIMARY KEY (Guid)
8
) TYPE=InnoDB; 根据该表写了一个User类,并且加上了XDoclet的tag。XDoclet有关hibernate的tag可以参考:
http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html
1
package sample.hibernate;
2
3
import java.io.Serializable;
4
5
/**//**
6
* <p>Title: </p>
7
*
8
* <p>Description: </p>
9
*
10
* <p>Copyright: Copyright (c) 2005</p>
11
*
12
* <p>Company: </p>
13
*
14
* @author George Hill
15
* @version 1.0
16
*/
17
18
/**//**
19
* @hibernate.class
20
* table="User"
21
* dynamic-update="true"
22
* dynamic-insert="true"
23
*/
24
public class User implements Serializable
{
25
26
// identifier field
27
private int guid;
28
29
// persistent field
30
private String account;
31
32
// persistent field
33
private String password;
34
35
// persistent field
36
private String email;
37
38
/**//**
39
* default constructor
40
*/
41
public User()
{}
42
43
/**//**
44
* full constructor
45
*/
46
public User(String account, String password, String email)
{
47
this.account = account;
48
this.password = password;
49
this.email = email;
50
}
51
52
/**//**
53
* @hibernate.id
54
* generator-class="native"
55
* type="int"
56
* column="Guid"
57
*/
58
public int getGuid()
{
59
return guid;
60
}
61
62
public void setGuid(int guid)
{
63
this.guid = guid;
64
}
65
66
/**//**
67
* @hibernate.property
68
* column="Account"
69
* length="64"
70
* not-null="true"
71
*/
72
public String getAccount()
{
73
return account;
74
}
75
76
public void setAccount(String account)
{
77
this.account = account;
78
}
79
80
/**//**
81
* @hibernate.property
82
* column="Password"
83
* length="16"
84
* not-null="true"
85
*/
86
public String getPassword()
{
87
return password;
88
}
89
90
public void setPassword(String password)
{
91
this.password = password;
92
}
93
94
/**//**
95
* @hibernate.property
96
* column="Email"
97
* length="128"
98
* not-null="true"
99
*/
100
public String getEmail()
{
101
return email;
102
}
103
104
public void setEmail(String email)
{
105
this.email = email;
106
}
107
108
}
109
为了生成hbm.xml文件,需要用到ant,下面是我的build.xml文件的内容:
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
3
<project name="Hibernate XDoclet Examples" default="hibernate" basedir=".">
4
<property name="xdoclet.root.dir" value="D:/lib/xdoclet-1.2.3"/>
5
<property name="xdoclet.lib.dir" value="${xdoclet.root.dir}/lib"/>
6
7
<path id="sampleclasspath">
8
<fileset dir="${xdoclet.lib.dir}">
9
<include name="*.jar"/>
10
</fileset>
11
</path>
12
13
<taskdef
14
name="hibernatedoclet"
15
classname="xdoclet.modules.hibernate.HibernateDocletTask"
16
classpathref="sampleclasspath"
17
/>
18
19
<target name="hibernate" description="Generate mapping documents">
20
21
<echo>+---------------------------------------------------+</echo>
22
<echo>| |</echo>
23
<echo>| R U N N I N G H I B E R N A T E D O C L E T |</echo>
24
<echo>| |</echo>
25
<echo>+---------------------------------------------------+</echo>
26
27
<hibernatedoclet
28
destdir="./src"
29
excludedtags="@version,@author,@todo,@see"
30
addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"
31
force="false"
32
verbose="true">
33
34
<fileset dir="./src">
35
<include name="sample/hibernate/*.java"/>
36
</fileset>
37
38
<hibernate version="2.1"/>
39
40
</hibernatedoclet>
41
</target>
42
</project>
43
build.xml文件中的第四行是指定XDoclet的根路径,需要根据解压的路径做相应的修改。XDoclet的下载地址:
http://xdoclet.sourceforge.net/xdoclet/install.html。在taskdef里面需要指定classname为xdoclet.modules.hibernate.HibernateDocletTask。在34行的fileset里面需要指定你的Java类所在的包路径。
运行ant,可以看到生成的User.hbm.xml文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="sample.hibernate.User"
table="User"
dynamic-update="true"
dynamic-insert="true"
>

<id
name="guid"
column="Guid"
type="int"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="account"
type="java.lang.String"
update="true"
insert="true"
column="Account"
length="64"
not-null="true"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="Password"
length="16"
not-null="true"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="Email"
length="128"
not-null="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

posted on 2005-05-17 10:20
小米 阅读(933)
评论(0) 编辑 收藏 所属分类:
Hibernate