hibernate多对多级联删除总结
A. 应用场景:
一个简单的权限系统的例子:
有三张主表:
用户表:t_user
角色表:role
菜单表:menu
有两张关系表:
用户角色关系表:UserRoleMap
角色菜单关系表:RoleMenuMap
role表和t_user表,及role表和menu表通过两张关系表进行多对多关联。
目标是:当删除某个角色的时候,级联删除两张关系表中对应的关系记录
B. 下面是hibernate的实现:
UserRoleMap.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse - Hibernate Tools--><hibernate-mapping> <class name="com.cuishen.edwview.pojo.sys.UserRoleMap" table="userrolemap" catalog="edwindex"> <composite-id> <key-property name="userId" type="java.lang.Long"> <column name="userid" /> </key-property> <key-property name="roleId" type="java.lang.Long"> <column name="roleid" /> </key-property> </composite-id> </class></hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.cuishen.edwview.pojo.sys.RoleMenuMap" table="rolemenumap"><composite-id><key-property name="roleId" column="roleid" type="java.lang.Long"/> <key-property name="menuId" column="menuid" type="java.lang.Long"/></composite-id></class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse - Hibernate Tools--><hibernate-mapping> <class name="com.cuishen.edwview.pojo.sys.Role" table="role" catalog="edwindex"><id name="id" type="java.lang.Long" column="id"><generator type="string"><column name="name" length="20" not-null="true" /></property><set name="menus" table="RoleMenuMap" inverse="false" lazy="false" order-by="roleId, menuId"><key column="roleId"/><many-to-many column="menuId" table="UserRoleMap" inverse="false" lazy="false"><key column="roleId"/><many-to-many column="userId" 才回过神来。
辛苦啊!~<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse - Hibernate Tools--><hibernate-mapping> <class name="com.cuishen.edwview.pojo.sys.UserRoleMap" table="userrolemap" catalog="edwindex"> <composite-id> <key-property name="userId" type="java.lang.Long"> <column name="userid" /> </key-property> <key-property name="roleId" type="java.lang.Long"> <column name="roleid" /> </key-property> </composite-id> </class></hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.cuishen.edwview.pojo.sys.RoleMenuMap" table="rolemenumap"><composite-id><key-property name="roleId" column="roleid" type="java.lang.Long"/> <key-property name="menuId" column="menuid" type="java.lang.Long"/></composite-id></class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse - Hibernate Tools--><hibernate-mapping> <class name="com.cuishen.edwview.pojo.sys.Role" table="role" catalog="edwindex"><id name="id" type="java.lang.Long" column="id"><generator type="string"><column name="name" length="20" not-null="true" /></property><set name="menus" table="RoleMenuMap" inverse="false" lazy="false" order-by="roleId, menuId"><key column="roleId"/><many-to-many column="menuId" table="UserRoleMap" inverse="false" lazy="false"><key column="roleId"/><many-to-many column="userId" class="com.cuishen.edwview.pojo.sys.User"/></set> </class></hibernate-mapping>