1. 在实际开发中,多对多关联比较常见,比如用户和角色,
或者角色和权限。角色和权限的关系:
2. 类结构: Permission.java
public class Permission { private int id; private String name; private Set<Role> roles; public Permission() { // TODO Auto-generated constructor stub } public Permission(String name) { super(); this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
Role.java
public class Role { private int id; private String name; private Set<Permission> permissions=new HashSet<Permission>(); public Role() { // TODO Auto-generated constructor stub } public Role(String name) { super(); this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Permission> getPermissions() { return permissions; } public void setPermissions(Set<Permission> permissions) { this.permissions = permissions; } }
3. 映射文件: Permission.hbm.xml :
<hibernate-mapping> <class name="cn.wh.vo.Permission" table="t_permission"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <set name="roles" table="t_role_permission" inverse="true"> <key column="pid"></key> <many-to-many column="rid" class="cn.sxt.vo.Role"></many-to-many> </set> </class> </hibernate-mapping>
Role.hbm.xml
<hibernate-mapping package="cn.wh.vo"> <class name="Role" table="t_role"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <!-- table表示 关联表 这张表由hibernate来创建和维护--> <set name="permissions" table="t_role_permission"> <!-- 当前类在关联表中的外键 --> <key column="rid"></key> <!-- 表示当前类的关联类在关联表中的外键 --> <many-to-many column="pid" class="Permission"/> </set> </class> </hibernate-mapping>
4. 测试:
public class HibernateTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数 是否将脚本导入数据库执行 export.create(true, true); } @Test public void testInit(){ Transaction tx = null; try { tx = session.beginTransaction(); Role r1 = new Role("管理员"); Role r2 = new Role("会员"); Permission p1 = new Permission("系统管理"); Permission p2 = new Permission("用户管理"); Permission p3 = new Permission("订单管理"); Permission p4 = new Permission("商品管理"); r1.getPermissions().add(p1); r1.getPermissions().add(p2); r1.getPermissions().add(p3); r1.getPermissions().add(p4); r2.getPermissions().add(p3); r2.getPermissions().add(p4); session.save(p1); session.save(p2); session.save(p3); session.save(p4); session.save(r1); session.save(r2); tx.commit(); } catch (Exception e) { if(tx!=null) tx.rollback(); } } @Test public void testGet(){ Role r = (Role)session.get(Role.class, 1); System.out.println(r.getName()); System.out.println("--------------"); for(Permission p:r.getPermissions()){ System.out.println(p.getName()); } System.out.println("==============="); Permission p = (Permission)session.get(Permission.class, 3); System.out.println(p.getName()); for(Role r1:p.getRoles()){ System.out.println(r1.getName()); } } }
publicclass HibernateTest { private Session session=null; @Before publicvoid setUp(){ session = HibernateUtil.getSession(); } @After publicvoid tearDown(){ HibernateUtil.close(); } @Test publicvoid testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数是否将脚本导入数据库执行 export.create(true, true); } @Test publicvoid testInit(){ Transaction tx = null; try { tx = session.beginTransaction(); Role r1 = new Role("管理员"); Role r2 = new Role("会员"); Permission p1 = new Permission("系统管理"); Permission p2 = new Permission("用户管理"); Permission p3 = new Permission("订单管理"); Permission p4 = new Permission("商品管理"); r1.getPermissions().add(p1); r1.getPermissions().add(p2); r1.getPermissions().add(p3); r1.getPermissions().add(p4); r2.getPermissions().add(p3); r2.getPermissions().add(p4); session.save(p1); session.save(p2); session.save(p3); session.save(p4); session.save(r1); session.save(r2); tx.commit();
} catch (Exception e) { if(tx!=null) tx.rollback(); } } @Test publicvoid testGet(){ Role r = (Role)session.get(Role.class, 1); System.out.println(r.getName()); System.out.println("--------------"); for(Permission p:r.getPermissions()){ System.out.println(p.getName()); } System.out.println("==============="); Permission p = (Permission)session.get(Permission.class, 3); System.out.println(p.getName()); for(Role r1:p.getRoles()){ System.out.println(r1.getName()); } } } |