在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我们我们的需求是能通过角色获取到其所拥有的操作权限,这就构成了单项的多对多的映射关系,为了管理这个关系,已经不能再通过添加外键列,必须在建立一张关系表,专门负责角色和权限之间的关系映射,如下:

这里假设有两个用户一个是管理员admin,一个是普通用户user,管理员拥有全部四个权限,而普通用户只拥有用户管理和资料管理的权限,这种关系在hibernate中应该这样配置:
新建Function实体类:
public class Function { private int id; private String name; //get/set方法省略}新建Role实体类:
public class Role { private int id; private String name; private Set<Function> sets=new HashSet<Function>(); //get/set方法省略} 在当前包下新建Function类的映射文件Function.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.wang.pojo"> <class name="Function" > <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class></hibernate-mapping>在当前包下新建Role类的映射文件Role.hbm.xml:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.wang.pojo"> <class name="Role" > <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <!-- 单向多对多 --> <set name="sets" table="role_func" cascade="save-update"> <!-- 表示当前类映射到关系表中的列 --> <key column="roleId"></key> <!-- 所对应的另一方在关系表中的列 --> <many-to-many column="funcId" class="Function"></many-to-many> </set> </class></hibernate-mapping>将两个映射文件添加到hibernate.cfg.xml中. 新建一个测试类.测试1:自动生成表2:保存数据3:读取数据:
@Test public void testCreateDB() { Configuration cfg = new Configuration().configure(); SchemaExport se = new SchemaExport(cfg); // 第一个参数是否生成ddl脚本 第二个参数是否执行到数据库 se.create(true, true); } @Test public void testSave() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Function f1=new Function(); f1.setName("用户管理"); Function f2=new Function(); f2.setName("资料管理"); Function f3=new Function(); f3.setName("系统管理"); Function f4=new Function(); f4.setName("权限管理"); Role r1=new Role(); r1.setName("admin"); r1.getSets().add(f1); r1.getSets().add(f2); r1.getSets().add(f3); r1.getSets().add(f4); Role r2=new Role(); r2.setName("user"); r2.getSets().add(f1); r2.getSets().add(f2); session.save(r1); session.save(r2); tx.commit(); session.close(); } @Test public void testGet() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Role r=(Role)session.get(Role.class, 1); System.out.println("用户:"+r.getName()); Iterator<Function> it = r.getSets().iterator(); while(it.hasNext()){ System.out.print(" "+it.next().getName()); } tx.commit(); session.close(); }以上是单向多对多映射关系的代码,如果我们既需要通过查询Role得到Function中的数据,又需要通过查询Fucntion来得到Role中数据,这样就构成了双向的多对多的映射关系,在上面的基础上需要做如下修改:
1.在Function实体类中添加 private Set<Role> roles=new HashSet<Role>(); 以及对应get/set方法.
2.在Function.hbm.xml中,添加以下代码:
<!-- 单向多对多 设置由另一方来维护关系--> <set name="roles" table="role_func" inverse="true" cascade="save-update"> <!-- 表示当前类映射到关系表中的列 --> <key column="funcId"></key> <!-- 所对应的另一方在关系表中的列 --> <many-to-many column="roleId" class="Role"></many-to-many> </set>,这样就可以实现从任何一方读取数据都能读到对应的另一方的数据信息了.
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。




