多對多關係(方式一) <hibernate-mapping package="com.bjsxt.hibernate.po"> 學生和課程多對多 <class name="Course" table="t_course"> <id name="courseId" column="course_id"> <generator class="assigned"></generator> </id> <property name="cname"/> <set name="stuSet" table="stu_course" > // stu_course爲中間表 <key column="course_id"></key> // course_id做爲stu_course的外鍵 <many-to-many class="Students" column="stu_id"></many-to-many> </set> </class> </hibernate-mapping> <hibernate-mapping package="com.bjsxt.hibernate.po"> <class name="Students" table="t_students" > <id name="stuId" column="stu_id"> <generator class="assigned"></generator> </id> <property name="stuName" column="stu_name" /> <property name="age" /> <property name="email" /> <set name="courseSet" table="stu_course" >// stu_course爲中間表 <key column="stu_Id"></key> //stu_id做爲stu_course的外鍵 <many-to-many class="Course" column="course_id" ></many-to-many> </set> </class> </hibernate-mapping> 多對多關係(方式二) <hibernate-mapping package="com.bjsxt.hibernate.po"> <class name="Course" table="t_Course" > <id name="courseId" column="course_id"> <generator class="assigned"></generator> </id> <property name="cname" /> <set name="stuCourseSet" > <key column="course_id"></key>// course_id做爲stuCourseSet表的外鍵 <one-to-many class="StudentsCourse"></one-to-many> //課程表和中間表的關係爲一對多 </set> </class> </hibernate-mapping> <hibernate-mapping package="com.bjsxt.hibernate.po"> <class name="Students" table="t_students" > <id name="stuId" column="stu_id"> <generator class="assigned"></generator> </id> <property name="stuName" column="stu_name" /> <property name="age" /> <property name="email" /> <set name="stuCourseSet"> <key column="stu_Id"></key>// stu_Id做爲stuCourseSet表的外鍵 <one-to-many class="StudentsCourse"></one-to-many> //學生表和中間表的關係爲一對多 </set> </class> </hibernate-mapping> 一對多關係 <hibernate-mapping package="com.bjsxt.hibernate.po"> 部門和員工一對多 <class name="Department" table="t_department"> <id name="deptId" column="dept_id"> <generator class="assigned"></generator> </id> <property name="dname" /> <set name="empSet" inverse="false" lazy="false" cascade="save-update"> <key column="dept_id"></key> // dept_id爲員工表裏的外鍵 <one-to-many class="Employee"/> </set> </class> </hibernate-mapping> <hibernate-mapping package="com.bjsxt.hibernate.po"> <class name="Employee" table="t_employee" > <id name="empId" column="emp_id"> <generator class="assigned"></generator> </id> <property name="ename" /> <property name="job" /> <property name="hiredate" /> <many-to-one name="department" column="dept_id" ></many-to-one>// dept_id爲員工表裏的外鍵 </class> </hibernate-mapping> 一對一關係 <hibernate-mapping package="com.bjsxt.hibernate.po"> 結婚中的男人和女人一對一 <class name="Man" table="t_man" > <id name="manId" column="man_id"> <generator class="foreign"> //外鍵加上惟一性約束 <param name="property">woman</param> </generator> </id> <property name="manName" column="man_Name"/> <one-to-one name="woman" class="Woman" ></one-to-one> </class> </hibernate-mapping> <hibernate-mapping package="com.bjsxt.hibernate.po"> <class name="Woman" table="t_woman"> <id name="womanId" column="woman_id"> <generator class="assigned"></generator> //關係由男人表維護,女表裏沒有男id </id> <property name="wname" /> <one-to-one name="man" class="Man" cascade="all" ></one-to-one> </class> </hibernate-mapping>