Hibernate級聯關係問題
有 Department,Post,DepartmentPost三張表。DepartmentPost為一張中間表,用於關聯Department與Post的關係。。Department與DepartmentPost是一對多的關係。
Department配置檔案
<set name="deptPosts" lazy="true" cascade="all-delete-orphan" inverse="true"
>
<key column="DEPARTMENT_ID"/>
<one-to-many class="com.reach.po.department.DepartmentPost"/>
</set>
DepartmentPost配置檔案
<many-to-one name="department"
column="DEPARTMENT_ID"
lazy="false"
class="com.reach.po.department.Department" />
one-to-many的一方,inverse設定為true ,不主動控制many一方。在儲存department時,需要手動去執行, deptPost.setDepartment(dept);
deptPosts.add(deptPost);
dept.setDeptPosts(deptPosts);
若將一方的inverse設定為false,則控制many方,在儲存department時,只需要
deptPosts.add(deptPost);
dept.setDeptPosts(deptPosts);
但是這樣每次操作department都會更新DepartmentPost,會影響效率