角色实体

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 岗位
 * 
 * @author tyg
 * 
 */
public class Role {
    private Long id;
    private String name;
    private String description;
    private Set<User> users = new HashSet<User>();
    private Set<Privilege> privileges = new HashSet<Privilege>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<Privilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(Set<Privilege> privileges) {
        this.privileges = privileges;
    }

}


权限实体

package cn.itcast.oa.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 权限
 * 
 * @author tyg
 * 
 */
public class Privilege {
    private Long id;
    private String url;
    private String name; // 权限名称
    private Set<Role> roles = new HashSet<Role>();
    private Privilege parent; // 上级权限
    private Set<Privilege> children = new HashSet<Privilege>(); // 下级权限

    public Privilege() {
    }

    public Privilege(String name, String url, Privilege parent) {
        this.name = name;
        this.url = url;
        this.parent = parent;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

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

    public Privilege getParent() {
        return parent;
    }

    public void setParent(Privilege parent) {
        this.parent = parent;
    }

    public Set<Privilege> getChildren() {
        return children;
    }

    public void setChildren(Set<Privilege> children) {
        this.children = children;
    }

}


两者映射

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.oa.domain">

    <class name="Role" table="itcast_role">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" />
        <property name="description" />
        
        
        <!-- users属性,本类与User的多对多 -->
        <set name="users" table="itcast_user_role">
            <key column="roleId"></key>
            <many-to-many class="User" column="userId"></many-to-many>
        </set>
        
        
        <!-- privileges属性,本类与Privilege的多对多 -->
        <set name="privileges" table="itcast_role_privilege" lazy="false">
            <key column="roleId"></key>
            <many-to-many class="Privilege" column="privilegeId"></many-to-many>
        </set>    
        
    </class>
    
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.oa.domain">

    <class name="Privilege" table="itcast_privilege">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="url" />
        <property name="name" />
        
        
        <!-- roles属性,本类与Role的多对多-->
        <set name="roles" table="itcast_role_privilege">
            <key column="privilegeId"></key>
            <many-to-many class="Role" column="roleId"></many-to-many>
        </set>


        <!-- parent属性,本类与Privilege(上级)的多对一-->
        <many-to-one name="parent" class="Privilege" column="parentId"></many-to-one>


        <!-- children属性,本类与Privilege(下级)的一对多-->
        <set name="children" lazy="false">
            <key column="parentId"></key>
            <one-to-many class="Privilege"/>
        </set>

        
    </class>
    
</hibernate-mapping>


Hibernate实体映射总结

<!-- users属性,本类与User的一对多 -->
格式:?属性,本类与?的?
?1    属性名
?2    关联对类型
?3    关系

    多对一:
        <many-to-one name="" class="" column=""></many-to-one>
    一对多(Set):
        <set name="">
            <key column=""></key>
            <one-to-many class=""/>
        </set>
    多对多(Set):
        <set name="" table="">
            <key column=""></key>
            <many-to-many class="" column=""></many-to-many>
        </set>