看代码说话:)
我们举个简单的例子,一个blog有用户User,文章BlogEntity,文章分类Category以及现在很流行的Tag.(为了简单,这里例子举的是单用户,及不需要考虑Category,Tag与User的对应关系)

1:一个User对应多篇BlogEntity
2:一篇BlogEntity可以对应多个Tag,对应多个Category,对应一个User
3:一个Category对应多个BlogEntity
4:一个Tag对应多个BlogEntity

OK,这四个对象简单的关系为:
1:User和BlogEntity是一对多的关系
2:BlogEntity和Category是多对多的关系
3:BlogEntity和Tag是多对多的关系

User.java

None.gif package  martin.xpost.model;
None.gif
None.gif
import  martin.xpost.hibernate.UserDAO;
None.gif
import  martin.xpost.util.ListUtil;
None.gif
None.gif
import  java.util.Set;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 
@author martin
InBlock.gif * 
@version 1.0
InBlock.gif * @hibernate.class table="t_user"
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  User  extends  PersistentImpl  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property not-null="true"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String userName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property not-null="true"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String password;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String realName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String email;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.set table="t_blogentity" lazy="true" inverse="true" cascade="all-delete-orphan"
InBlock.gif     * @hibernate.key column="userid"
InBlock.gif     * @hibernate.one-to-many class="martin.xpost.model.BlogEntity"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private Set blogEntities;
InBlock.gif
InBlock.gif    
//----------------------------------------------------------------
InBlock.gif
    /// getter and setter
InBlock.gif    
//----------------------------------------------------------------
ExpandedBlockEnd.gif
}


BlogEntity.java

None.gif package  martin.xpost.model;
None.gif
None.gif
import  java.util.Set;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 
@author martin
InBlock.gif * 
@version 1.0
InBlock.gif * @hibernate.class table="t_blogentity"
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  BlogEntity  extends  PersistentImpl  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property not-null="true"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String title;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String shortBrief;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String content;
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.many-to-one column="userid" not-null="true"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private User user;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
InBlock.gif     * @hibernate.key column="blogentityid"
InBlock.gif     * @hibernate.many-to-many class="martin.xpost.model.Category" column="categoryid"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private Set categories;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
InBlock.gif     * @hibernate.key column="blogentityid"
InBlock.gif     * @hibernate.many-to-many class="martin.xpost.model.Tag" column="tagid"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private Set tags;
InBlock.gif
InBlock.gif    
//----------------------------------------------------------------
InBlock.gif
    /// getter and setter
InBlock.gif    
//----------------------------------------------------------------
ExpandedBlockEnd.gif
}

Category.java

None.gif package  martin.xpost.model;
None.gif
None.gif
import  java.util.Set;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 
@author martin
InBlock.gif * @hibernate.class table="t_category"
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  Category  extends  PersistentImpl  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property not-null="true"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String categoryName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String description;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
InBlock.gif     * @hibernate.key column="categoryid"
InBlock.gif     * @hibernate.many-to-many class="martin.model.xpost.BlogEntity" column="blogentityid"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private Set blogEntities;
InBlock.gif
InBlock.gif    
//----------------------------------------------------------------
InBlock.gif
    /// getter and setter
InBlock.gif    
//----------------------------------------------------------------
ExpandedBlockEnd.gif
}

Tag.java
None.gif package  martin.xpost.model;
None.gif
None.gif
import  java.util.Set;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif * 
@author martin
InBlock.gif * 
@version 1.0
InBlock.gif * @hibernate.class table="t_tag"
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  Tag  extends  PersistentImpl  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.property
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private String tagName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * @hibernate.set table="t_blogentity_tag"  lazy="true" cascade="none"
InBlock.gif     * @hibernate.key column="tagid"
InBlock.gif     * @hibernate.many-to-many class="martin.xpost.model.BlogEntity" column="blogentityid"
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    
private Set blogEntities;
InBlock.gif
InBlock.gif    
//----------------------------------------------------------------
InBlock.gif
    /// getter and setter
InBlock.gif    
//----------------------------------------------------------------
ExpandedBlockEnd.gif
}

----------------------------------------------------------------------------
----------------------------------------------------------------------------
好了,这个没什么好说的,看代码就知道怎么用了,下面我们通过ant生成hbm.xml文件.
一:下载xdoclet 2: http://xdoclet.codehaus.org

二:编写ant脚本
None.gif <? xml version="1.0" ?>
None.gif
< project  name ="xpost"  default ="init" >
None.gif    
< property  name ="src.java.dir"  value ="src" />
None.gif
None.gif    
< property  name ="build.dir"  value ="WEB-INF/classes" />
None.gif    
< property  name ="hbm.dir"  value ="WEB-INF/classes" />
None.gif
None.gif    
< property  name ="xdoclet.lib.dir"  value ="build/lib/xdoclet" />
None.gif    
< property  name ="build.lib.dir"  value ="build/lib/runtime" />
None.gif
None.gif    
< path  id ="xdoclet.class.path" >
None.gif        
< fileset  dir ="${xdoclet.lib.dir}" >
None.gif            
< include  name ="**/*.jar" />
None.gif        
</ fileset >
None.gif    
</ path >
None.gif
None.gif    
< path  id ="build.class.path" >
None.gif        
< fileset  dir ="${build.lib.dir}" >
None.gif            
< include  name ="**/*.jar" />
None.gif        
</ fileset >
None.gif    
</ path >
None.gif
None.gif    
< target  name ="init" >
None.gif        
< mkdir  dir ="${build.dir}" />
None.gif    
</ target >
None.gif
None.gif    
< target  name ="compile" >
None.gif        
< javac  srcdir ="${src.java.dir}"
None.gif               destdir
="${build.dir}"
None.gif               classpathref
="build.class.path" />
None.gif    
</ target >
None.gif
None.gif    
< target  name ="removehbm" >
None.gif        
< delete  dir ="${src.java.dir}"  includes ="**/model/*.xml" />
None.gif    
</ target >
None.gif
None.gif    
< target  name ="hibernatedoclet"
None.gif            depends
="removehbm"
None.gif            description
="Generate Persistence and form classes" >
None.gif
None.gif        
< taskdef
None.gif                
name ="xdoclet"
None.gif                classname
="org.xdoclet.ant.XDocletTask"
None.gif                classpathref
="xdoclet.class.path" />
None.gif        
< xdoclet >
None.gif            
<!--  defines the file handled by xdoclet2  -->
None.gif            
< fileset  dir ="${src.java.dir}" >
None.gif                
< include  name ="**/model/*.java" />
None.gif            
</ fileset >
None.gif            
<!--  defines the processing of a plugin  -->
None.gif            
< component
None.gif                    
classname ="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
None.gif                    destdir
="${src.java.dir}"
None.gif                    version
="3.0" />
None.gif        
</ xdoclet >
None.gif    
</ target >
None.gif
None.gif    
< target
None.gif            
name ="jarhbm"
None.gif            depends
="hibernatedoclet" >
None.gif        
< jar
None.gif                
basedir ="${src.java.dir}"
None.gif    &nb