1. For example, in rights management, the relationship between the roles and permissions is many-table structure is:
2. Class structure
Role.java
public class Role implements Serializable{ private int id; private String name; private Set<Permission> permissions=new HashSet<Permission>(); public Role() { } 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; } }
Permisssion.java
public class Permission implements Serializable{ 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; } }
3. mapping file information
Role.hbm.xml
<hibernate-mapping package="cn.sxt.pojo"> <class name="Role" table="t_role"> <id name="id"> <generator class="native"> </generator> </id> <property name="name"/> <set name="permissions" table="t_role_permission"> <!-- 当前类在连接表中的外键 --> <key column="rid"></key> <many-to-many column="pid" class="Permission"></many-to-many> </set> </class> </hibernate-mapping>
Permission.hbm.xml
<hibernate-mapping package="cn.sxt.pojo"> <class name="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="Role"></many-to-many> </set> </class> </hibernate-mapping>
4. Test
public class HibernateTest { / ** * generation of database tools method * * / @Test public void testCreateDB () { the Configuration CFG = new new the Configuration () Configure ();. the SchemaExport SE = new new the SchemaExport (CFG); // first sql script print parameters whether // if the second parameter to the database exported script execution se.create ( to true , to true ); } / ** * initialization table data * / @Test public voidtestInit () { the Session the session = null ; the Transaction TX = null ; the try { the session = HibernateUtil.getSession (); TX = session.beginTransaction (); the Permission P1 = new new the Permission ( "add user" ); the Permission P2 = new new the Permission ( "Remove user" ); Permission p3 = new new Permission ( 'query user " ); Permission P4 = new new Permission (' modify user"); Role r1 = new Role("管理员"); r1.getPermissions().add(p1); r1.getPermissions().add(p2); r1.getPermissions().add(p3); r1.getPermissions().add(p4); Role r2 = new Role("vip"); 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(); }finally { HibernateUtil.close(); } } /** */ @Test public void testGetData(){ Session session = HibernateUtil.getSession(); Role role = (Role)session.get(Role.class, 1); System.out.println(role.getId()+"---"+role.getName()); System.out.println("-------------"); for(Permission p:role.getPermissions()){ System.out.println(p.getId()+"---"+p.getName()); } System.out.println("=========================="); Permission p = (Permission)session.get(Permission.class, 3); System.out.println(p.getId()+"----"+p.getName()); for(Role r:p.getRoles()){ System.out.println(r.getId()+"---"+r.getName()); } HibernateUtil.close(); } }