Table of Contents
集合映射
关联映射
多对一映射与一对多
Inverse属性
cascade 属性
多对多映射
维护关联关系
Home Database Mysql Tutorial Hibernate关联映射

Hibernate关联映射

Jun 07, 2016 pm 02:49 PM
hibernate association develop mapping process gather need

集合映射 开发流程: 需求分析/数据库设计、项目设计/ 编码/测试/实施部署上线/验收需求: 用户购买, 填写地址! // javabean设计 public class User { private int userId; private String userName; // 一个用户,对应的多个地址 private Set String addre

集合映射

<code>开发流程:
    需求分析/数据库设计、项目设计/ 编码/测试/实施部署上线/验收

需求:
    用户购买, 填写地址!
</code>
Copy after login
<code class=" hljs lasso"><span class="hljs-comment">// javabean设计</span>
<span class="hljs-keyword">public</span> class User {

    <span class="hljs-keyword">private</span> int userId;
    <span class="hljs-keyword">private</span> <span class="hljs-built_in">String</span> userName;
    <span class="hljs-comment">// 一个用户,对应的多个地址</span>
    <span class="hljs-keyword">private</span> <span class="hljs-built_in">Set</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> address;
    <span class="hljs-keyword">private</span> <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> addressList <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> ArrayList<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>(); 
    <span class="hljs-comment">//private String[] addressArray; // 映射方式和list一样     <array name=""></array></span>
    <span class="hljs-keyword">private</span> <span class="hljs-built_in">Map</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span>,<span class="hljs-built_in">String</span><span class="hljs-subst">></span> addressMap <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> HashMap<span class="hljs-subst"><</span><span class="hljs-built_in">String</span>, <span class="hljs-built_in">String</span><span class="hljs-subst">></span>();

}
</code>
Copy after login
<code class=" hljs xml"><span class="hljs-comment"><!-- 映射文件 --></span>
<span class="hljs-pi"><?xml version="1.0"?></span>
<span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span>

<span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.a_collection"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"User"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_user"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"userId"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"id"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>   
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"userName"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>

        <span class="hljs-comment"><!-- 
            set集合属性的映射
                name 指定要映射的set集合的属性
                table 集合属性要映射到的表
                key  指定集合表(t_address)的外键字段
                element 指定集合表的其他字段
                    type 元素类型,一定要指定
         --></span>
         <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_address"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span>></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span>
         <span class="hljs-tag"></<span class="hljs-title">set</span>></span>

         <span class="hljs-comment"><!-- 
            list集合映射
                list-index  指定的是排序列的名称 (因为要保证list集合的有序)
          --></span>
          <span class="hljs-tag"><<span class="hljs-title">list</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressList"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_addressList"</span>></span>
              <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
              <span class="hljs-tag"><<span class="hljs-title">list-index</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"idx"</span>></span><span class="hljs-tag"></<span class="hljs-title">list-index</span>></span>
              <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span>></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span>
          <span class="hljs-tag"></<span class="hljs-title">list</span>></span>

          <span class="hljs-comment"><!-- 
            map集合的映射
                key  指定外键字段
                map-key 指定map的key 
                element  指定map的value
           --></span>
          <span class="hljs-tag"><<span class="hljs-title">map</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressMap"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_addressMap"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">map-key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"shortName"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span> ></span><span class="hljs-tag"></<span class="hljs-title">map-key</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span> ></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span>
          <span class="hljs-tag"></<span class="hljs-title">map</span>></span>         
    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Copy after login
<code class="java hljs ">    <span class="hljs-comment">// 测试程序</span>
    <span class="hljs-comment">// 保存set</span>
    <span class="hljs-annotation">@Test</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSaveSet</span>() <span class="hljs-keyword">throws</span> Exception {
        Session session = sf.openSession();
        session.beginTransaction();

        <span class="hljs-comment">//-- 保存</span>
        Set<String> addressSet = <span class="hljs-keyword">new</span> HashSet<String>();
        addressSet.add(<span class="hljs-string">"广州"</span>);
        addressSet.add(<span class="hljs-string">"深圳"</span>);
        <span class="hljs-comment">// 用户对象</span>
        User user = <span class="hljs-keyword">new</span> User();
        user.setUserName(<span class="hljs-string">"Jack"</span>);
        user.setAddress(addressSet);

        <span class="hljs-comment">// 保存</span>
        session.save(user);

        session.getTransaction().commit();
        session.close();
    }

    <span class="hljs-comment">// 保存list/map</span>
    <span class="hljs-annotation">@Test</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSaveList</span>() <span class="hljs-keyword">throws</span> Exception {
        Session session = sf.openSession();
        session.beginTransaction();
        User user = <span class="hljs-keyword">new</span> User();
        user.setUserName(<span class="hljs-string">"Tom"</span>);
<span class="hljs-comment">//      // 用户对象  --  list</span>
<span class="hljs-comment">//      user.getAddressList().add("广州");</span>
<span class="hljs-comment">//      user.getAddressList().add("深圳");</span>
<span class="hljs-comment">//      // 保存</span>
<span class="hljs-comment">//      session.save(user);</span>

        <span class="hljs-comment">// 用户对象  --  Map</span>
        user.getAddressMap().put(<span class="hljs-string">"A0001"</span>, <span class="hljs-string">"广州"</span>);
        user.getAddressMap().put(<span class="hljs-string">"A0002"</span>, <span class="hljs-string">"深圳"</span>);

        <span class="hljs-comment">// 保存</span>
        session.save(user);

        session.getTransaction().commit();
        session.close();
    }
</code>
Copy after login
<code>问题:
    集合映射,映射的集合元素,都是普通的类型, 能否为对象类型?
</code>
Copy after login

关联映射

<code>需求1:
    部门与员工
          一个部门有多个员工;       【一对多】
          多个员工,属于一个部门    【多对一】
需求2:
    项目与开发员工
        一个项目,有多个开发人员!
        一个开发人员,参与多个项目!   【多对多】
</code>
Copy after login

多对一映射与一对多

<code>1. 需求:员工与部门
2. 数据库:
3. 设计javabean封装:
4. 映射:
</code>
Copy after login

这里写图片描述

JavaBean

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Dept {

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> deptId;
    <span class="hljs-keyword">private</span> String deptName;
    <span class="hljs-comment">// 【一对多】 部门对应的多个员工</span>
    <span class="hljs-keyword">private</span> Set<Employee> emps = <span class="hljs-keyword">new</span> HashSet<Employee>();
}   
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Employee {

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> empId;
    <span class="hljs-keyword">private</span> String empName;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> salary;
    <span class="hljs-comment">// 【多对一】员工与部门</span>
    <span class="hljs-keyword">private</span> Dept dept;
}</code>
Copy after login

Dept.hbm.xml

<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span>
<span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span>

<span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.b_one2Many"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Dept"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_dept"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"deptId"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>   
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"deptName"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>

        <span class="hljs-comment"><!-- 
            一对多关联映射配置  (通过部门管理到员工)
            Dept 映射关键点:
            1.  指定 映射的集合属性: "emps"
            2.  集合属性对应的集合表: "t_employee"
            3.  集合表的外键字段   "t_employee. dept_id"
            4.  集合元素的类型

         --></span>
         <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"emps"</span>></span>   <span class="hljs-comment"><!-- table="t_employee" --></span>
             <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"dept_id"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
             <span class="hljs-tag"><<span class="hljs-title">one-to-many</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Employee"</span>/></span>
         <span class="hljs-tag"></<span class="hljs-title">set</span>></span>      
    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Copy after login

Employee.hbm.xml

<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span>
<span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span>

<span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.b_one2Many"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Employee"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_employee"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"empId"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>   
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"empName"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"salary"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"double"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>

        <span class="hljs-comment"><!-- 
            多对一映射配置
            Employee 映射关键点:
            1.  映射的部门属性  :  dept
            2.  映射的部门属性,对应的外键字段: dept_id
            3.  部门的类型
         --></span>
         <span class="hljs-tag"><<span class="hljs-title">many-to-one</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"dept"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"dept_id"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Dept"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-one</span>></span>       
    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Copy after login

测试

<code class=" hljs java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> {</span>

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SessionFactory sf;
    <span class="hljs-keyword">static</span> {
        sf = <span class="hljs-keyword">new</span> Configuration()
            .configure()
            .addClass(Dept.class)   
            .addClass(Employee.class)   <span class="hljs-comment">// 测试时候使用</span>
            .buildSessionFactory();
    }

    <span class="hljs-comment">// 保存, 部门方 【一的一方法操作】</span>
    <span class="hljs-annotation">@Test</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span>() {

        Session session = sf.openSession();
        session.beginTransaction();

        <span class="hljs-comment">// 部门对象</span>
        Dept dept = <span class="hljs-keyword">new</span> Dept();
        dept.setDeptName(<span class="hljs-string">"应用开发部"</span>);
        <span class="hljs-comment">// 员工对象</span>
        Employee emp_zs = <span class="hljs-keyword">new</span> Employee();
        emp_zs.setEmpName(<span class="hljs-string">"张三"</span>);
        Employee emp_ls = <span class="hljs-keyword">new</span> Employee();
        emp_ls.setEmpName(<span class="hljs-string">"李四"</span>);
        <span class="hljs-comment">// 关系</span>
        dept.getEmps().add(emp_zs);
        dept.getEmps().add(emp_ls);

        <span class="hljs-comment">// 保存</span>
        session.save(emp_zs);
        session.save(emp_ls);
        session.save(dept); <span class="hljs-comment">// 保存部门,部门下所有的员工  </span>

        session.getTransaction().commit();
        session.close();
        <span class="hljs-comment">/*
         *  结果
         *  Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
            Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
            Hibernate: insert into t_dept (deptName) values (?)
            Hibernate: update t_employee set deptId=? where empId=?    维护员工引用的部门的id
            Hibernate: update t_employee set deptId=? where empId=?
         */</span>
    }
    <span class="hljs-comment">// 【推荐】 保存, 部员方 【多的一方法操作】</span>
    <span class="hljs-annotation">@Test</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save2</span>() {

        Session session = sf.openSession();
        session.beginTransaction();

        <span class="hljs-comment">// 部门对象</span>
        Dept dept = <span class="hljs-keyword">new</span> Dept();
        dept.setDeptName(<span class="hljs-string">"综合部"</span>);
        <span class="hljs-comment">// 员工对象</span>
        Employee emp_zs = <span class="hljs-keyword">new</span> Employee();
        emp_zs.setEmpName(<span class="hljs-string">"张三"</span>);
        Employee emp_ls = <span class="hljs-keyword">new</span> Employee();
        emp_ls.setEmpName(<span class="hljs-string">"李四"</span>);
        <span class="hljs-comment">// 关系</span>
        emp_zs.setDept(dept);
        emp_ls.setDept(dept);


        <span class="hljs-comment">// 保存</span>
        session.save(dept); <span class="hljs-comment">// 先保存一的方法</span>
        session.save(emp_zs);
        session.save(emp_ls);<span class="hljs-comment">// 再保存多的一方,关系回自动维护(映射配置完)</span>

        session.getTransaction().commit();
        session.close();
        <span class="hljs-comment">/*
         *  结果
         *  Hibernate: insert into t_dept (deptName) values (?)
            Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
            Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
            少生成2条update  sql
         */</span>
    }
}</code>
Copy after login

总结

<code>  在一对多与多对一的关联关系中,保存数据最好的通过多的一方来维护关系,这样可以减少update语句的生成,从而提高hibernate的执行效率!

配置一对多与多对一,这种叫“双向关联”
只配置一对多,           叫“单项一对多”
只配置多对一,           叫“单项多对一”

注意:
    配置了哪一方,哪一方才有维护关联关系的权限!
</code>
Copy after login

Inverse属性

<code>Inverse属性,是在维护关联关系的时候起作用的。
       表示控制权是否转移。(在一的一方起作用)

Inverse , 控制反转。
Inverse = false  不反转;   当前方有控制权
        True  控制反转; 当前方没有控制权

维护关联关系中,是否设置inverse属性:

    1. 保存数据 
        有影响。
       如果设置控制反转,即inverse=true, 然后通过部门方维护关联关系。在保存部门的时候,同时保存员工, 数据会保存,但关联关系不会维护。即外键字段为NULL
</code>
Copy after login

这里写图片描述

<code>    2. 获取数据
        无。
    3. 解除关联关系?
        有影响。
        inverse=false,  可以解除关联
        inverse=true,  当前方(部门)没有控制权,不能解除关联关系
        (不会生成update语句,也不会报错)
    4. 删除数据对关联关系的影响?
        有影响。
        inverse=false, 有控制权, 可以删除。先清空外键引用,再删除数据。
        inverse=true,  没有控制权: 如果删除的记录有被外键引用,会报错,违反主外键引用约束!  如果删除的记录没有被引用,可以直接删除。
</code>
Copy after login

cascade 属性

<code>cascade  表示级联操作  【可以设置到一的一方或多的一方】
    none                  不级联操作, 默认值
    save-update           级联保存或更新
    delete                级联删除
    save-update,delete    级联保存、更新、删除
    all                   同上。级联保存、更新、删除
</code>
Copy after login

多对多映射

<code>需求:项目与开发人员
      Project   Developer
      电商系统
           曹吉
           王春
       OA系统
            王春
            老张
</code>
Copy after login

这里写图片描述

JavaBean

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Developer {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> d_id;
    <span class="hljs-keyword">private</span> String d_name;
    <span class="hljs-comment">// 开发人员,参数的多个项目</span>
    <span class="hljs-keyword">private</span> Set<Project> projects = <span class="hljs-keyword">new</span> HashSet<Project>();
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Project {
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> prj_id;
    <span class="hljs-keyword">private</span> String prj_name;
    <span class="hljs-comment">// 项目下的多个员工</span>
    <span class="hljs-keyword">private</span> Set<Developer> developers = <span class="hljs-keyword">new</span> HashSet<Developer>();
}</code>
Copy after login

Project.hbm.xml

<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span>
<span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span>

<span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.c_many2many"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Project"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_project"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"prj_id"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>   
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"prj_name"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>
        <span class="hljs-comment"><!-- 
            多对多映射:
            1.  映射的集合属性: “developers”
            2.  集合属性,对应的中间表: “t_relation”
            3. 外键字段:  prjId
            4. 外键字段,对应的中间表字段:  did
            5.   集合属性元素的类型
         --></span>
         <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"developers"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_relation"</span> <span class="hljs-attribute">cascade</span>=<span class="hljs-value">"save-update"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"prjId"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">many-to-many</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"did"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Developer"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-many</span>></span>
         <span class="hljs-tag"></<span class="hljs-title">set</span>></span>      
    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Copy after login

Developer.hbm.xml

<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span>
<span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span>

<span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.c_many2many"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Developer"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_developer"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"d_id"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">id</span>></span>   
        <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"d_name"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span>

        <span class="hljs-comment"><!-- 
            多对多映射配置: 员工方
                name  指定映射的集合属性
                table 集合属性对应的中间表
                key   指定中间表的外键字段(引用当前表t_developer主键的外键字段)
                many-to-many
                    column 指定外键字段对应的项目字段
                    class  集合元素的类型
         --></span>
        <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"projects"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_relation"</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"did"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span>
            <span class="hljs-tag"><<span class="hljs-title">many-to-many</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"prjId"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Project"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-many</span>></span>
        <span class="hljs-tag"></<span class="hljs-title">set</span>></span>       
    <span class="hljs-tag"></<span class="hljs-title">class</span>></span>
<span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Copy after login

维护关联关系

<code>设置inverse属性,在多对多种维护关联关系的影响?

1) 保存数据
    有影响。
    inverse=false ,有控制权,可以维护关联关系; 保存数据的时候会把对象关系插入中间表;
    inverse=true,  没有控制权, 不会往中间表插入数据。

2) 获取数据
    无。

3) 解除关系
    有影响。
    inverse=false ,有控制权, 解除关系就是删除中间表的数据。
    inverse=true, 没有控制权,不能解除关系。

4) 删除数据
    有影响。
    inverse=false, 有控制权。 先删除中间表数据,再删除自身。
    inverse=true, 没有控制权。 如果删除的数据有被引用,会报错! 否则,才可以删除
</code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to open multiple Toutiao accounts? What is the process for applying for a Toutiao account? How to open multiple Toutiao accounts? What is the process for applying for a Toutiao account? Mar 22, 2024 am 11:00 AM

With the popularity of mobile Internet, Toutiao has become one of the most popular news information platforms in my country. Many users hope to have multiple accounts on the Toutiao platform to meet different needs. So, how to open multiple Toutiao accounts? This article will introduce in detail the method and application process of opening multiple Toutiao accounts. 1. How to open multiple Toutiao accounts? The method of opening multiple Toutiao accounts is as follows: On the Toutiao platform, users can register accounts through different mobile phone numbers. Each mobile phone number can only register one Toutiao account, which means that users can use multiple mobile phone numbers to register multiple accounts. 2. Email registration: Use different email addresses to register a Toutiao account. Similar to mobile phone number registration, each email address can also register a Toutiao account. 3. Log in with third-party account

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ​​such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Mar 21, 2024 pm 04:41 PM

In today's fast-paced society, sleep quality problems are plaguing more and more people. In order to improve users' sleep quality, a group of special sleep anchors appeared on the Douyin platform. They interact with users through live broadcasts, share sleep tips, and provide relaxing music and sounds to help viewers fall asleep peacefully. So, are these sleep anchors profitable? This article will focus on this issue. 1. Are Douyin sleep anchors profitable? Douyin sleep anchors can indeed earn certain profits. First, they can receive gifts and transfers through the tipping function in the live broadcast room, and these benefits depend on their number of fans and audience satisfaction. Secondly, the Douyin platform will give the anchor a certain share based on the number of views, likes, shares and other data of the live broadcast. Some sleep anchors will also

Which Linux distribution is best for Android development? Which Linux distribution is best for Android development? Mar 14, 2024 pm 12:30 PM

Android development is a busy and exciting job, and choosing a suitable Linux distribution for development is particularly important. Among the many Linux distributions, which one is most suitable for Android development? This article will explore this issue from several aspects and give specific code examples. First, let’s take a look at several currently popular Linux distributions: Ubuntu, Fedora, Debian, CentOS, etc. They all have their own advantages and characteristics.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

See all articles