首页 > 代码库 > (十三)Hibernate中的多表操作(3):单向多对多

(十三)Hibernate中的多表操作(3):单向多对多

  • 多对多的处理方式是,有一张中间表,中间表保存两个多方之间的关系。首先来看实际应用场景:在之前开发的系统中,应用了基于角色的控制访问,也就是RBAC模型,一个用户可能存在多种角色,一种角色也可能有多个用户,所以用户和角色之间是一个多对多的关系。

 

 


 

案例一:  使用注解方式实现多对多关系

  • RoleBean.java
package bean;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "role")
public class RoleBean implements Serializable {

    @Id
    private Integer role_id;
    private String role_name;
    private String role_remark;

    @ManyToMany
    @JoinTable(name = "role_menu",    // //name=”role_menu”中间表的名称是“role_menu”
    joinColumns = { @JoinColumn(name = "roleid") },    //设置当前实体在中间表中的映射,也就是说在中间表中有一个字段叫roleid,使它指向当前表的主键这里是role_id
    inverseJoinColumns = { @JoinColumn(name = "menu_id") })  //设置对方实体在中间中的映射,也就是说在中间表中有一个字段menu_id,使它指向对方表(menu表)的主键。
    private Set<MenuBean> menuSet = new HashSet<MenuBean>();

    public RoleBean(Integer role_id, String role_name, String role_remark) {
        super();
        this.role_id = role_id;
        this.role_name = role_name;
        this.role_remark = role_remark;
    }

    public RoleBean() {
    }

    public Integer getRole_id() {
        return role_id;
    }

    public void setRole_id(Integer role_id) {
        this.role_id = role_id;
    }

    public String getRole_name() {
        return role_name;
    }

    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }

    public String getRole_remark() {
        return role_remark;
    }

    public void setRole_remark(String role_remark) {
        this.role_remark = role_remark;
    }

    public Set<MenuBean> getMenuSet() {
        return menuSet;
    }

    public void setMenuSet(Set<MenuBean> menuSet) {
        this.menuSet = menuSet;
    }

}
  • MenuBean.java
package bean;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "menu")
public class MenuBean implements Serializable {
    @Id
    private Integer menuid;
    private String menuname;

    public MenuBean() {
    }

    public MenuBean(Integer menuid, String menuname) {
        super();
        this.menuid = menuid;
        this.menuname = menuname;
    }

    public Integer getMenuid() {
        return menuid;
    }

    public void setMenuid(Integer menuid) {
        this.menuid = menuid;
    }

    public String getMenuname() {
        return menuname;
    }

    public void setMenuname(String menuname) {
        this.menuname = menuname;
    }

}

  • 把两个含有注解的bean文件添加到总配置文件里即可实现。



 

案例二:使用xml配置文件实现单向多对多

  • 创建实体类  Role.java
package bean;

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

/**
 * Role entity. @author MyEclipse Persistence Tools
 */

public class Role implements java.io.Serializable {

    // Fields

    private Integer roleId;
    private String roleName;
    private String roleRemark;

    private Set<MenuBean> menuSet=new HashSet<MenuBean>();
    // Constructors

    /** default constructor */
    public Role() {
    }

    /** minimal constructor */
    public Role(Integer roleId) {
        this.roleId = roleId;
    }

    /** full constructor */
    public Role(Integer roleId, String roleName, String roleRemark) {
        this.roleId = roleId;
        this.roleName = roleName;
        this.roleRemark = roleRemark;
    }

    // Property accessors

    public Integer getRoleId() {
        return this.roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return this.roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleRemark() {
        return this.roleRemark;
    }

    public void setRoleRemark(String roleRemark) {
        this.roleRemark = roleRemark;
    }

    public Set<MenuBean> getMenuSet() {
        return menuSet;
    }

    public void setMenuSet(Set<MenuBean> menuSet) {
        this.menuSet = menuSet;
    }
    
}
  • 创建实体类 MenuBean.java
package bean;

/**
 * MenuBean entity. @author MyEclipse Persistence Tools
 */

public class MenuBean implements java.io.Serializable {

    // Fields

    private Integer menuid;
    private String menuname;

    /** default constructor */
    public MenuBean() {
    }

    /** minimal constructor */
    public MenuBean(Integer menuid) {
        this.menuid = menuid;
    }

    /** full constructor */
    public MenuBean(Integer menuid, String menuname) {
        this.menuid = menuid;
        this.menuname = menuname;
    }

    // Property accessors

    public Integer getMenuid() {
        return this.menuid;
    }

    public void setMenuid(Integer menuid) {
        this.menuid = menuid;
    }

    public String getMenuname() {
        return this.menuname;
    }

    public void setMenuname(String menuname) {
        this.menuname = menuname;
    }

}
  • 把创建实体类的映射文件 Role.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
    <class name="bean.Role" table="role" catalog="test">
        <id name="roleId" type="java.lang.Integer">
            <column name="role_id" />
            <generator class="assigned"></generator>
        </id>
        <property name="roleName" type="java.lang.String">
            <column name="role_name" />
        </property>
        <property name="roleRemark" type="java.lang.String">
            <column name="role_remark" />
        </property>
 <!-- 单向多对多 -->
        <set name="menuSet" table="role_menu">
            <key column="roleid"></key>     <!-- 表示当前类映射到关系表中的列 -->
            <many-to-many   column="menu_id"  class="bean.MenuBean" ></many-to-many>   <!-- 所对应的另一方在关系表中的列 -->
        </set>

    </class>
</hibernate-mapping>
  • MenuBean.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="bean.MenuBean" table="menu" catalog="test">
        <id name="menuid" type="java.lang.Integer">
            <column name="menuid" />
            <generator class="assigned"></generator>
        </id>
        <property name="menuname" type="java.lang.String">
            <column name="menuname" />
        </property>
    </class>
</hibernate-mapping>
  • 把两个映射文件添加到总配置文件里即可实现。

 

(十三)Hibernate中的多表操作(3):单向多对多