XDoclet是一个文档生成工具,由最初的ejbdoc演变而来,恐怕连作者都没有想到Xdoclet能如此优秀~~(@_*),关于XDoclet的文档,官方有详细的参考.
1.环境:
a.ANT 1.6.5 http://ww.apache.org
b.XDoclet 1.2.3 http://xdoclet.sourcefoge.net
由于只是测试生成Hibernate的配置文件,并不要求Hibernate类库的支持.
2.代码:
两个POJO,演示了普通的many-to-one的映射关系.一个ANT的build.xml
UserInfo.java
1
package cn.neto.bo;
2
3
import java.util.Set;
4
/**
5
* @author phoenixup
6
* @hibernate.class table="user"
7
*/
8
public class UserInfo
9
{
10
private long id;
11
private String username;
12
private String password;
13
private String orgname;
14
private Set roles;
15
/**
16
* @hibernate.set role="roles"
17
*
18
*
19
* @hibernate.collection-key column="roleid"
20
*
21
* @hibernate.collection-one-to-many class="Role"
22
*/
23
public Set getRoles()
24
{
25
return roles;
26
}
27
public void setRoles(Set roles)
28
{
29
this.roles = roles;
30
}
31
/**
32
* @hibernate.property column="orgname" type="string" length="200"
33
*/
34
public String getOrgname()
35
{
36
return orgname;
37
}
38
public void setOrgname(String orgname)
39
{
40
this.orgname = orgname;
41
}
42
/**
43
* @hibernate.propery column="password" type="string" length="50"
44
*/
45
public String getPassword()
46
{
47
return password;
48
}
49
public void setPassword(String password)
50
{
51
this.password = password;
52
}
53
/**
54
* @hibernate.propery column="username" type="string" unique="true" length="50"
55
*/
56
public String getUsername()
57
{
58
return username;
59
}
60
public void setUsername(String username)
61
{
62
this.username = username;
63
}
64
public long getId()
65
{
66
return id;
67
}
68
/**
69
* @hibernate.id column="userid" generator-class="increment" type="long"
70
*/
71
public void setId(long id)
72
{
73
this.id = id;
74
}
75
76
}
77
package cn.neto.bo;2

3
import java.util.Set;4
/**5
* @author phoenixup6
* @hibernate.class table="user"7
*/8
public class UserInfo9
{10
private long id;11
private String username;12
private String password;13
private String orgname;14
private Set roles;15
/**16
* @hibernate.set role="roles"17
* 18
* 19
* @hibernate.collection-key column="roleid"20
* 21
* @hibernate.collection-one-to-many class="Role"22
*/23
public Set getRoles()24
{25
return roles;26
}27
public void setRoles(Set roles)28
{29
this.roles = roles;30
}31
/**32
* @hibernate.property column="orgname" type="string" length="200"33
*/34
public String getOrgname()35
{36
return orgname;37
}38
public void setOrgname(String orgname)39
{40
this.orgname = orgname;41
}42
/**43
* @hibernate.propery column="password" type="string" length="50"44
*/45
public String getPassword()46
{47
return password;48
}49
public void setPassword(String password)50
{51
this.password = password;52
}53
/**54
* @hibernate.propery column="username" type="string" unique="true" length="50"55
*/56
public String getUsername()57
{58
return username;59
}60
public void setUsername(String username)61
{62
this.username = username;63
}64
public long getId()65
{66
return id;67
}68
/**69
* @hibernate.id column="userid" generator-class="increment" type="long" 70
*/71
public void setId(long id)72
{73
this.id = id;74
}75
76
}77

Role.java
1
package cn.neto.bo;
2
3
/**
4
* @hibernate.class table="role"
5
*/
6
public class Role
7
{
8
private long roleid;
9
private UserInfo user;
10
private String rolename;
11
/**
12
* @hibernate.id column="roleid" generator-class="increment" type="long"
13
*/
14
public long getRoleid()
15
{
16
return roleid;
17
}
18
public void setRoleid(long roleid)
19
{
20
this.roleid = roleid;
21
}
22
/**
23
* @hibernate.property column="rolename" type="string" length="50"
24
* @return
25
*/
26
public String getRolename()
27
{
28
return rolename;
29
}
30
public void setRolename(String rolename)
31
{
32
this.rolename = rolename;
33
}
34
/**
35
* @hibernate.many-to-one column="userid" class="cn.neto.bo.UserInfo" not-null="true"
36
*/
37
public UserInfo getUser()
38
{
39
return user;
40
}
41
public void setUser(UserInfo user)
42
{
43
this.user = user;
44
}
45
}
46
package cn.neto.bo;2

3
/**4
* @hibernate.class table="role"5
*/6
public class Role7
{8
private long roleid;9
private UserInfo user;10
private String rolename;11
/**12
* @hibernate.id column="roleid" generator-class="increment" type="long"13
*/14
public long getRoleid()15
{16
return roleid;17
}18
public void setRoleid(long roleid)19
{20
this.roleid = roleid;21
}22
/**23
* @hibernate.property column="rolename" type="string" length="50"24
* @return25
*/26
public String getRolename()27
{28
return rolename;29
}30
public void setRolename(String rolename)31
{32
this.rolename = rolename;33
}34
/**35
* @hibernate.many-to-one column="userid" class="cn.neto.bo.UserInfo" not-null="true"36
*/37
public UserInfo getUser()38
{39
return user;40
}41
public void setUser(UserInfo user)42
{43
this.user = user;44
}45
}46

关于其中的@hibernate.*标记,请参考http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html的官方文档.
build.xml
<project default="xdoclet" basedir=".">
<property file="build.properties" />
<target name="init">
<mkdir dir="${bin.dir}" />
</target>
<target name="xdoclet" depends="init">
<taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask">
<classpath>
<fileset dir="${xdoclet.home}/lib">
<include name="*.jar" />
</fileset>
</classpath>
</taskdef>
<hibernatedoclet destdir="${bin.dir}" mergedir="${bin.dir}" force="true" excludedtags="@version,@author,@todo" addedtags="@xdoclet-generatored at ${TODAY}" verbose="false">
<fileset dir="./src">
<include name="**/bo/*.java" />
</fileset>
<hibernate version="2.1"/>
</hibernatedoclet>
</target>
</project>build.properties
#########################
#属性文件
#########################
#xdoclet的目录
xdoclet.home=e:/Tools/XDoclet/xdoclet-bin-1.2.3/xdoclet-1.2.3
#配置文件生成位置
bin.dir=./bin
#源文件位置
src.dir=./src
#hibernate的目录
hibernate.home=E:/Tools/Hibernate/hibernate-2.1.8/hibernate-2.1
3.运行:
命令行窗口输入:
4.FAQ
1.请正确配置系统的ANT_HOME,JAVA_HOME,保证能正常工作.
2.请注意你的Hibernate版本属性必须与你的Hibernate版本号对应,在build.xml文件中的 <hibernate version="2.1"/> .
5.结束:
本例仅做演示只用,让你在使用XDdoclet生成Hibernate配置文件的时候,有一个简单清晰的认识,以上范例在以下环境中调试成功,如果有兴趣的,可以自行操作.


浙公网安备 33010602011771号