The unidirectional hibernate-many association mapping of java

This one-way-many association mapping

1. How to rights management, the relationship between the roles and permissions that-many relationships, the table structure is:

2. Class structure

Permission.java

public class Permission implements Serializable{
    private int id;
    private String name;
    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;
    }
    
}

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;
    }
}

3. mapping file

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"/>
    </class>
</hibernate-mapping>

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>

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());
        }
        
        HibernateUtil.close();
    }
}

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11204760.html