Hibernate的多对多关系查询

Hibernate的多对多关系查询

多对多的条件

  1. 需求:通过ID查询用户的信息,同时查询用户对应的角色的信息。(先查询本表需要查询的id的信息)

    select * from sys_user u where u.user_id=5;
  2. 用户对应的角色的信息。(通过查询出来的外键字段去中间表查询对应的外键)

    • 注意:用户和角色的关系是多对多的关系,他们的关系是在中间表里面的
      select * from sys_user_role ur where ur.user_id=5;
  3. 通过中间表的映射关系找到了角色ID为 4,5(通过中间表查询出来的外键去对应关联的表查询信息)

    select * from sys_role s where s.role_id in(4,5);
    • 对多对通用一个表找到另一个表的数据的条件为(重点)
    • 中间表
    • 中间表对应本表的外键
    • 中间表对应关联表的外键

    • 问题:为什么需要三个条件

    • 答:因为多对多的时候,两个表的关系是通过中间表建立的!!


实现这个操作必须做两个步骤:

  1. 创建一个可以存储多个表数据的实体类,User实体配置多对多的关系
//用户和角色是多对多的关系
//意味着一个用户可以有多个角色,那么使用集合Set接收角色的信息
private Set<Role> roles=new HashSet<Role>();

public Set<Role> getRoles() {
    return roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
  1. 将查询的多个表的数据封装这个实体累的对象里面,通过配置来说实现。多对多配置

    • set标签: 作用是用于配置实体类对应的set集合

    • name : 唯一标识符,就是指定实体类对应的属性名是哪个set集合,也代表返回的类型是一个set集合

    • table:多对多的时候对应的中间表的表名

    • key标签 : 多对多的配置时,指定 本配置的实体类对应的表在中间表的外键
    • many-to-many标签:作用 声明是多对多的关系
    • class:指定集合的元素的数据类型
    • column:指定间表对应关联表的外键
<!-- 多对多的关系 

-1.中间表
-2.中间表对应本表的外键
-3.中间表对应关联表的外键
set标签的作用是指定返回的类似是一个set,和实体类的声明保持一致
name:实体类的对应set集合的属性名
table:多对多的时候对应的中间表的表名
-->
<set name="roles" table="sys_user_role">
   <!--本配置文件对应的表在中间表的外键  -->
   <key column="user_id"></key>
   <!-- 声明多对多的关系 -->
   <!-- 
    many-to-many:声明是多对多的关系
   class:配置集合元素的类型
   column:中间表对应关联表的外键
    -->
   <many-to-many class="com.entity.Role" column="role_id"></many-to-many>
</set>

代码

entity实体类

User

public class User {
    private Long userId;
    private String userCode;
    private String userName;
    private String userPassword;
    private char userState;

    private Set<Role> roles;

    public Set<Role> getRoles() {
        return roles;
    }
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
    get()/set()......
}

Role

public class Role {
    private Long roleId;
    private String roleName;
    private String roleMemo;

    private Set<User> users;

    public Set<User> getUsers() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }
    get()/set()......
}

配置文件

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.entity.User" table="sys_user">
    <id name="userId" column="user_id">
        <generator class="identity"></generator>
    </id>
    <property name="userCode" column="user_code"></property>
    <property name="userName" column="user_name"></property>
    <property name="userPassword" column="user_password"></property>
    <property name="userState" column="user_state"></property>

    <!-- 
        set代表返回的类型是一个set集合
        name指定实体类对应对应的属性名是哪一个set集合
        table指定中间表的表名
     -->
    <set name="roles" table="sys_user_role">
        <!-- 
            指定本配置类的对应中间表的字段名
        -->
        <key column="user_id"></key>
        <!-- 
            many-to-many声明是一个多对多的关系
            class指定的是返回的数据类型
            column代表的中间表中外键的字段名
        -->
        <many-to-many class="com.entity.Role" column="role_id"></many-to-many>
    </set>
</class>
</hibernate-mapping>

Role.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.entity.Role" table="sys_role">
    <id name="roleId" column="role_id">
        <generator class="identity"></generator>
    </id>
    <property name="roleName" column="role_name"></property>
    <property name="roleMemo" column="role_memo"></property>

    <set name="users" table="sys_user_role">
        <key column="role_id"></key>
        <many-to-many class="com.entity.User" column="user_id"></many-to-many>
    </set>
</class>
</hibernate-mapping>

测试方法

//需求:通过ID查询用户的信息,同时查询用户对应的角色的信息。
@Test
public void get(){

    //1.获得操作对象
    Session session = HibernateUtils.getSession();
    //2.通过OID获得对象
    User user = session.get(User.class, 5L);
    System.out.println("用户名:"+user.getUserName());

    Set<Role> roles = user.getRoles();
    for(Role role:roles){
        System.out.println("角色名:"+role.getRoleName());
    }

    //6.关闭连接
    session.close();
}

猜你喜欢

转载自blog.csdn.net/kato_op/article/details/80304681