看代码说话:)
我们举个简单的例子,一个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
package
martin.xpost.model;
import martin.xpost.hibernate.UserDAO;
import martin.xpost.util.ListUtil;
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_user"
*/
public class User extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String userName;
/**
* @hibernate.property not-null="true"
*/
private String password;
/**
* @hibernate.property
*/
private String realName;
/**
* @hibernate.property
*/
private String email;
/**
* @hibernate.set table="t_blogentity" lazy="true" inverse="true" cascade="all-delete-orphan"
* @hibernate.key column="userid"
* @hibernate.one-to-many class="martin.xpost.model.BlogEntity"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
import martin.xpost.hibernate.UserDAO;
import martin.xpost.util.ListUtil;
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_user"
*/
public class User extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String userName;
/**
* @hibernate.property not-null="true"
*/
private String password;
/**
* @hibernate.property
*/
private String realName;
/**
* @hibernate.property
*/
private String email;
/**
* @hibernate.set table="t_blogentity" lazy="true" inverse="true" cascade="all-delete-orphan"
* @hibernate.key column="userid"
* @hibernate.one-to-many class="martin.xpost.model.BlogEntity"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
BlogEntity.java
package
martin.xpost.model;
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_blogentity"
*/
public class BlogEntity extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String title;
/**
* @hibernate.property
*/
private String shortBrief;
/**
* @hibernate.property
*/
private String content;
/**
* @hibernate.many-to-one column="userid" not-null="true"
*/
private User user;
/**
* @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
* @hibernate.key column="blogentityid"
* @hibernate.many-to-many class="martin.xpost.model.Category" column="categoryid"
*/
private Set categories;
/**
* @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
* @hibernate.key column="blogentityid"
* @hibernate.many-to-many class="martin.xpost.model.Tag" column="tagid"
*/
private Set tags;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_blogentity"
*/
public class BlogEntity extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String title;
/**
* @hibernate.property
*/
private String shortBrief;
/**
* @hibernate.property
*/
private String content;
/**
* @hibernate.many-to-one column="userid" not-null="true"
*/
private User user;
/**
* @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
* @hibernate.key column="blogentityid"
* @hibernate.many-to-many class="martin.xpost.model.Category" column="categoryid"
*/
private Set categories;
/**
* @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
* @hibernate.key column="blogentityid"
* @hibernate.many-to-many class="martin.xpost.model.Tag" column="tagid"
*/
private Set tags;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
Category.java
package
martin.xpost.model;
import java.util.Set;
/**
* @author martin
* @hibernate.class table="t_category"
*/
public class Category extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String categoryName;
/**
* @hibernate.property
*/
private String description;
/**
* @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
* @hibernate.key column="categoryid"
* @hibernate.many-to-many class="martin.model.xpost.BlogEntity" column="blogentityid"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
import java.util.Set;
/**
* @author martin
* @hibernate.class table="t_category"
*/
public class Category extends PersistentImpl {
/**
* @hibernate.property not-null="true"
*/
private String categoryName;
/**
* @hibernate.property
*/
private String description;
/**
* @hibernate.set table="t_category_blogentity" lazy="true" cascade="none"
* @hibernate.key column="categoryid"
* @hibernate.many-to-many class="martin.model.xpost.BlogEntity" column="blogentityid"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
Tag.java
package
martin.xpost.model;
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_tag"
*/
public class Tag extends PersistentImpl {
/**
* @hibernate.property
*/
private String tagName;
/**
* @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
* @hibernate.key column="tagid"
* @hibernate.many-to-many class="martin.xpost.model.BlogEntity" column="blogentityid"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
import java.util.Set;
/**
* @author martin
* @version 1.0
* @hibernate.class table="t_tag"
*/
public class Tag extends PersistentImpl {
/**
* @hibernate.property
*/
private String tagName;
/**
* @hibernate.set table="t_blogentity_tag" lazy="true" cascade="none"
* @hibernate.key column="tagid"
* @hibernate.many-to-many class="martin.xpost.model.BlogEntity" column="blogentityid"
*/
private Set blogEntities;
//----------------------------------------------------------------
/// getter and setter
//----------------------------------------------------------------
}
----------------------------------------------------------------------------
----------------------------------------------------------------------------
好了,这个没什么好说的,看代码就知道怎么用了,下面我们通过ant生成hbm.xml文件.
一:下载xdoclet 2: http://xdoclet.codehaus.org
二:编写ant脚本
<?
xml version="1.0"
?>
< project name ="xpost" default ="init" >
< property name ="src.java.dir" value ="src" />
< property name ="build.dir" value ="WEB-INF/classes" />
< property name ="hbm.dir" value ="WEB-INF/classes" />
< property name ="xdoclet.lib.dir" value ="build/lib/xdoclet" />
< property name ="build.lib.dir" value ="build/lib/runtime" />
< path id ="xdoclet.class.path" >
< fileset dir ="${xdoclet.lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >
< path id ="build.class.path" >
< fileset dir ="${build.lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >
< target name ="init" >
< mkdir dir ="${build.dir}" />
</ target >
< target name ="compile" >
< javac srcdir ="${src.java.dir}"
destdir ="${build.dir}"
classpathref ="build.class.path" />
</ target >
< target name ="removehbm" >
< delete dir ="${src.java.dir}" includes ="**/model/*.xml" />
</ target >
< target name ="hibernatedoclet"
depends ="removehbm"
description ="Generate Persistence and form classes" >
< taskdef
name ="xdoclet"
classname ="org.xdoclet.ant.XDocletTask"
classpathref ="xdoclet.class.path" />
< xdoclet >
<!-- defines the file handled by xdoclet2 -->
< fileset dir ="${src.java.dir}" >
< include name ="**/model/*.java" />
</ fileset >
<!-- defines the processing of a plugin -->
< component
classname ="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
destdir ="${src.java.dir}"
version ="3.0" />
</ xdoclet >
</ target >
< target
name ="jarhbm"
depends ="hibernatedoclet" >
< jar
basedir ="${src.java.dir}"
&nb
< project name ="xpost" default ="init" >
< property name ="src.java.dir" value ="src" />
< property name ="build.dir" value ="WEB-INF/classes" />
< property name ="hbm.dir" value ="WEB-INF/classes" />
< property name ="xdoclet.lib.dir" value ="build/lib/xdoclet" />
< property name ="build.lib.dir" value ="build/lib/runtime" />
< path id ="xdoclet.class.path" >
< fileset dir ="${xdoclet.lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >
< path id ="build.class.path" >
< fileset dir ="${build.lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >
< target name ="init" >
< mkdir dir ="${build.dir}" />
</ target >
< target name ="compile" >
< javac srcdir ="${src.java.dir}"
destdir ="${build.dir}"
classpathref ="build.class.path" />
</ target >
< target name ="removehbm" >
< delete dir ="${src.java.dir}" includes ="**/model/*.xml" />
</ target >
< target name ="hibernatedoclet"
depends ="removehbm"
description ="Generate Persistence and form classes" >
< taskdef
name ="xdoclet"
classname ="org.xdoclet.ant.XDocletTask"
classpathref ="xdoclet.class.path" />
< xdoclet >
<!-- defines the file handled by xdoclet2 -->
< fileset dir ="${src.java.dir}" >
< include name ="**/model/*.java" />
</ fileset >
<!-- defines the processing of a plugin -->
< component
classname ="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
destdir ="${src.java.dir}"
version ="3.0" />
</ xdoclet >
</ target >
< target
name ="jarhbm"
depends ="hibernatedoclet" >
< jar
basedir ="${src.java.dir}"
&nb