我使用的數據庫是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;

2

3

4

5

6

7

8

根據該表寫了一個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

2

3

4

5


6

7

8

9

10

11

12

13

14

15

16

17

18


19

20

21

22

23

24



25

26

27

28

29

30

31

32

33

34

35

36

37

38


39

40

41



42

43


44

45

46



47

48

49

50

51

52


53

54

55

56

57

58



59

60

61

62



63

64

65

66


67

68

69

70

71

72



73

74

75

76



77

78

79

80


81

82

83

84

85

86



87

88

89

90



91

92

93

94


95

96

97

98

99

100



101

102

103

104



105

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

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文件的內容如下:




































































