首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate多对多级联删除小结

2013-11-09 
hibernate多对多级联删除总结A. 应用场景:一个简单的权限系统的例子:有三张主表:用户表:t_user角色表:role

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>


RoleMenuMap.hbm.xml
<?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>


Role.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.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>


RoleMenuMap.hbm.xml
<?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>


Role.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.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>


注意上面Role.hbm.xml中的inverse="false",加上这句配置后,在delete(role);的时候hibernate会自动维护关系表,将已不存在的对应关系删除,如果想在删除关系的同时也删除对应的User或者Menu,就需要再加上cascade="delete"项配置。

当然可以在DDL建表时加入外键级联更新的定义,这样就全权交由数据库来级联删除,就不需要hibernate再操心了!

热点排行