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"就能实现级联删除了。

你可能感兴趣的:(java,spring,Hibernate,qq,ssh)