Emp.javahtml
public class Emp{ private int id; private String name; }Dept.java
public class Dept{ private int id; private String name; Set<Emp> emps = new HashSet<Emp>(); }Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> </class>Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <set name="emps" cascade="all" inverse="true"> <key column="dept_id"></key> <one-to-many class="com.gec.cn.dao.domain.Emp" /> </set> </class>5. 单向多对一
public class Emp{ private int id; private String name; private Dept dept; }Dept.java
public class Dept{ private int id; private String name; }Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <many-to-one name="dept" column="dept_id" class="com.gec.cn.dao.domain.Dept"></many-to-one> </class>Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> </class>6 双向多对一(一对多)
public class Dept{ private int id; private String name; Set<Emp> emps = new HashSet<Emp>(); }Dept.java
public class Emp{ private int id; private String name; private Dept dept; }Emp.hbm.xml
<class name="com.gec.cn.dao.domain.Emp" table="emp"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <many-to-one name="dept" column="dept_id" class="com.gec.cn.dao.domain.Dept"></many-to-one> </class>Dept.hbm.xml
<class name="com.gec.cn.dao.domain.Dept" table="dept"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <set name="emps" cascade="all" inverse="true"> <key column="dept_id"></key> <one-to-many class="com.gec.cn.dao.domain.Emp" /> </set> </class>7. 多对多
public class Teacher{ private int id; private String name; Set<Student> stus = new HashSet<Student>(); }Student.java
public class Student{ private int id; private String name; Set<Teacher> teas = new HashSet<Teacher>(); }Student.hbm.xml
<class name="com.gec.cn.dao.domain.Student" table="student"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <property name="sex" column="sex" type="string"/> <property name="birthday" column="birthday" type="date"/> <set name="teachers" table="tea_stu"> <key column="stu_id"></key> <many-to-many class="com.gec.cn.dao.domain.Teacher" column="tea_id"></many-to-many> </set> </class>Teacher.hbm.xml
<class name="com.gec.cn.dao.domain.Teacher" table="teacher"> <id name="id" column="id" type="long"> <generator class="increment"/> </id> <property name="name" column="name" type="string"/> <property name="sex" column="sex" type="string"/> <property name="address" column="address" type="string"/> <set name="students" table="tea_stu"> <key column="tea_id"></key> <many-to-many class="com.gec.cn.dao.domain.Student" column="stu_id"></many-to-many> </set> </class>8. 一对一
public class Pserson{ private int id; private String name; private IdCard idcard; }IdCard.java
public class IdCard{ private int id; private String name; private Person person; }Person.hbm.xml
<class name="com.gec.cn.dao.daomain.Person" table="person"> <id name="id" column="id" type="long"> <generator class="increment" /> </id> <many-to-one name="idcard" class="com.gec.cn.dao.daomain.IDCard" column="card_id" cascade="all" unique="true"/> </class>IdCard.hbm.xml
<class name="com.gec.cn.dao.daomain.IDCard" table="idcard"> <id name="id" column="id" type="long"> <generator class="increment" /> </id> <property name="name" column="name" type="string"/> <property name="num" column="num" type="string"/> <property name="address" column="address" type="string"/> <one-to-one name="person" class="com.gec.cn.dao.daomain.Person" cascade="all"/> </class>(2)按主键映射
public class Pserson{ private int id; private String name; private IdCard idcard; }IdCard.java
public class IdCard{ private int id; private String name; private Person person; }Person.hbm.xml
<class name="com.gec.cn.dao.daomain.Person" table="tb_person"> <id name="id" column="id" type="long"> <generator class="foreign"> <param name="property">card</param> </generator> </id> <property name="name" column="name" type="string"></property> <one-to-one name="card" class="com.gec.cn.dao.daomain.Card" constrained="true"></one-to-one> </class>IdCard.hbm.xml
<class name="com.gec.cn.dao.daomain.Card" table="tb_card"> <id name="id" column="id" type="long"> <generator class="native" /> </id> <property name="num" column="num" type="string"/> <one-to-one name="person" class="com.gec.cn.dao.daomain.Person" cascade="all"/> </class>