Hibernate 刪除主表記錄並刪除副表記錄解決方法
公司的OA。採用SSH(Spring+Struts1.0+Hibernate) 小弟接觸Hibernate不久,寫的文章難免有些地方欠妥,如有不對之處,請給我留言或用QQ(941867223)聯絡我及時更改,以免誤導別人。謝謝!
資料庫表字段太多了,暫時不貼出來了,如有需求者用QQ聯絡我發到郵箱就行了:
我要刪除員工記錄功能
Employee.hbm.xml配置檔案如下:
<hibernate-mapping>
<class name="com.ww.crm.entity.Employee" table="employee" schema="dbo" catalog="crm11">
<id name="empId" type="java.lang.Integer">
<column name="emp_id" />
<generator class="identity"></generator>
</id>
<property name="empNo" type="java.lang.String">
<column name="emp_no" length="13" not-null="true" />
</property>
<property name="empName" type="java.lang.String">
<column name="emp_name" length="20" not-null="true" />
</property>
<property name="empLoginName" type="java.lang.String">
<column name="emp_login_name" length="16" not-null="true" />
</property>
<property name="empLoginPass" type="java.lang.String">
<column name="emp_login_pass" length="50" not-null="true" />
</property>
<property name="empSex" type="java.lang.Boolean">
<column name="emp_sex" />
</property>
<property name="empTell" type="java.lang.String">
<column name="emp_tell" length="13" />
</property>
<property name="empEmail" type="java.lang.String">
<column name="emp_email" length="50" />
</property>
<property name="empRemark" type="java.lang.String">
<column name="emp_remark" />
</property>
<property name="flageDelete" type="java.lang.Integer">
<column name="flageDelete" />
</property>
<property name="flageForDelete" type="java.lang.Integer">
<column name="flageForDelete" />
</property>
<set name="logservs" inverse="true" cascade="all-delete-orphan">
<key>
<column name="empId" />
</key>
<one-to-many class="com.ww.crm.entity.Logserv" />
</set>
<set name="integerFollows" inverse="true" cascade="all-delete-orphan">
<key>
<column name="ifempId" />
</key>
<one-to-many class="com.ww.crm.entity.IntegerFollow" />
</set>
<set name="serviceInfosForSerAssEmpId" inverse="true" cascade="all-delete-orphan">
<key>
<column name="ser_ass_emp_id" />
</key>
<one-to-many class="com.ww.crm.entity.ServiceInfo" />
</set>
<set name="serviceInfosForSerCreEmpId" inverse="true" cascade="all-delete-orphan" >
<key>
<column name="ser_cre_emp_id" not-null="true" />
</key>
<one-to-many class="com.ww.crm.entity.ServiceInfo" />
</set>
<set name="serviceInfosForSerDisPeoId" inverse="true" cascade="all-delete-orphan">
<key>
<column name="ser_dis_peo_id" />
</key>
<one-to-many class="com.ww.crm.entity.ServiceInfo" />
</set>
<set name="empRoles" inverse="true" cascade="all-delete-orphan">
<key>
<column name="emp_id" not-null="true" />
</key>
<one-to-many class="com.ww.crm.entity.EmpRole" />
</set>
<set name="clientInfosForRecievedEmp" inverse="true" cascade="all-delete-orphan">
<key>
<column name="recievedEmp" />
</key>
<one-to-many class="com.ww.crm.entity.ClientInfo" />
</set>
<set name="clientInfosForEmpId" inverse="true" cascade="all-delete-orphan">
<key>
<column name="emp_id" />
</key>
<one-to-many class="com.ww.crm.entity.ClientInfo" />
</set>
<set name="sellChancesForScDispatchMan" inverse="true" cascade="all-delete-orphan">
<key>
<column name="sc_dispatch_man" />
</key>
<one-to-many class="com.ww.crm.entity.SellChance" />
</set>
<set name="sellChancesForScCreateMan" inverse="true" cascade="all-delete-orphan">
<key>
<column name="sc_create_man" />
</key>
<one-to-many class="com.ww.crm.entity.SellChance" />
</set>
</class>
</hibernate-mapping>
RoleInfo.hbm.xml配置檔案如下:
<hibernate-mapping>
<class name="com.ww.crm.entity.RoleInfo" table="role_info" schema="dbo" catalog="crm11">
<id name="roleId" type="java.lang.Integer">
<column name="role_id" />
<generator class="identity"></generator>
</id>
<property name="roleName" type="java.lang.String">
<column name="role_name" length="30" not-null="true" />
</property>
<property name="roleRemark" type="java.lang.String">
<column name="role_remark" length="500" />
</property>
<property name="roleOther1" type="java.lang.String">
<column name="role_other1" length="50" />
</property>
<property name="roleOther2" type="java.lang.String">
<column name="role_other2" length="50" />
</property>
<set name="roleMenus" inverse="true" >
<key>
<column name="role_id" not-null="true" />
</key>
<one-to-many class="com.ww.crm.entity.RoleMenu" />
</set>
<set name="empRoles" inverse="true" cascade="all-delete-orphan">
<key>
<column name="role_id" not-null="true" />
</key>
<one-to-many class="com.ww.crm.entity.EmpRole" />
</set>
</class>
</hibernate-mapping>
EmpRole.hbm.xml配置檔案如下:
<hibernate-mapping>
<class name="com.ww.crm.entity.EmpRole" table="emp_role" schema="dbo" catalog="crm11">
<id name="erId" type="java.lang.Integer">
<column name="er_id" />
<generator class="identity"></generator>
</id>
<many-to-one name="roleInfo" class="com.ww.crm.entity.RoleInfo" fetch="select" lazy="false" >
<column name="role_id" not-null="true" />
</many-to-one>
<many-to-one name="employee" class="com.ww.crm.entity.Employee" fetch="select" lazy="false" cascade="all">
<column name="emp_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
此配置檔案是關聯員工和角色的中間表,在寫cascade="all"時,(注意RoleInfo不要寫cascade="call",不然要把此表中被刪除員工的角色相同的所有相關記錄都會被刪除。)像這樣就能把員工的記錄刪除了
總結:
1、在做級聯刪除時,一定要把被刪除的表之間關係弄清楚,不然hibernate就要刪除問題了。
2、在被刪除物件的配置檔案中,把與被刪除物件有關係的物件 在一對多<set >中設定cascade="all-delete-orphan"就能實現級聯刪除了。