• 一对多映射关系中使用List集合的写法以及List下标由谁维护


    例子:DeptEmp是一对多的关系

    Emp:

    public class Emp implements java.io.Serializable {

     

          private Integer id;

          private Dept dept;

          private String name;

          private int turn;

    ......

    }

    Dept

    public class Dept implements java.io.Serializable {

     

          private Integer id;

          private String name;

          private List<Emp> emps = new ArrayList();

    ......

    }

    Emp.hbm.xml:

    <many-to-one name="dept" class="Dept">

                <column name="dept_id" />

    </many-to-one>

    Dept.hbm.xml:

    <list name="emps" cascade="all">

                <key column="dept_id"/>

                <list-index column="turn" base="0"/>

                <one-to-many class="Emp" />

    </list>

    Session s = HbnUtil.getSession();

    Transaction transaction = s.getTransaction();

    transaction.begin();

              

    Emp emp = new Emp();

    emp.setName("kobe");

    Dept dept = new Dept();

    dept.setName("Lakers");

              

    emp.setDept(dept);

    dept.getEmps().add(emp);

              

    s.save(emp);

    s.save(dept);

              

    transaction.commit();

    如果<list>中添加属性inverse=”true”,则turn由手工维护,

    只有当inverse=”false”时,才是由hibernate自动维护turn。

    也就是下标必须由一的一方来维护。

  • 相关阅读:
    0.1+0.2!==0.3
    标准盒模型IE盒模型
    Vue自定义组件通过v-model通信
    vue-cli3.0 使用 postcss-pxtorem px转rem
    render函数、createElement函数
    mixins(混入)
    vue.extend与vue.component
    js事件系列
    vue脚手架项目结构
    python模块和包
  • 原文地址:https://www.cnblogs.com/mabaishui/p/1587671.html
Copyright © 2020-2023  润新知