zoukankan      html  css  js  c++  java
  • Hibernate的一对一关联映射

    一对一关联映射

    一、基于主键的一对一关联映射(双向)

    以公民表(tb_people)和身份证表(tb_idcard)为例:

    1. People.hbm.xml文件配置 (People类添加 private IDcard idcard;)

    <hibernate-mapping>
        <class name="com.cong.po.People" table="tb_people">
            <id name="id" column="id" type="int">
                <generator class="native"/>
            </id>
            <property name="name" type="string" length="45">
                <column name="name"/>
            </property>
            <property name="sex" type="string" length="2">
                <column name="sex"/>
            </property>
            <property name="age" type="int">
                <column name="age"/>
            </property>
            <one-to-one name="idcard" class="com.cong.po.IDcard" cascade="all"/>
        </class>
    </hibernate-mapping>

    注:cascade级联关系可以保证主控方所关联的被控方的操作的一致性
      例如:主控方进行save、update或delete操作时被控方会进行同样的操作

    2.IDcard.hbm.xml文件配置(IDcard类添加 private People people;)

    <hibernate-mapping>
        <class name="com.cong.po.IDcard" table="tb_idcard">
            <id name="id" column="id" type="int">
                <generator class="foreign">
                    <param name="property">people</param>
                </generator>
            </id>
            <property name="idcard_code" type="string" length="20" not-null="true">
                <column name="IDcard_code"/>
            </property>
            <one-to-one name="people" class="com.cong.po.People" constrained="true"/>
        </class>
    </hibernate-mapping>

    二、基于外键的一对一关联映射

    在tb_people中添加一个新字段card_id作为外键,同时保证该字段唯一性

    1.People.hbm.xml文件配置(People类添加 private int idcard;)

    <hibernate-mapping>
        <class name="com.cong.po.People" table="tb_people">
            <id name="id" column="id" type="int">
                <generator class="native"/>
            </id>
            <property name="name" type="string" length="45">
                <column name="name"/>
            </property>
            <property name="sex" type="string" length="2">
                <column name="sex"/>
            </property>
            <property name="age" type="int">
                <column name="age"/>
            </property>
            <many-to-one name="card_id" class="com.cong.po.IDcard" unique="true" not-null="true">
                <column name="card_id"/>
            </many-to-one>
        </class>
    </hibernate-mapping>

    2.IDcard.hbm.xml文件配置(IDcard类添加 private People people;)

    <hibernate-mapping>
        <class name="com.cong.po.IDcard" table="tb_idcard">
            <id name="id" column="id" type="int">
                <generator class="foreign">
                    <param name="property">people</param>
                </generator>
            </id>
            <property name="idcard_code" type="string" length="20" not-null="true">
                <column name="IDcard_code"/>
            </property>
        </class>
    </hibernate-mapping>

    注: 一对一外键关联实际就是多对一关联的一个特例,需要保证外键关联字段的唯一性
      在<many-to-one>元素中通过unique属性就可以实现关联字段的唯一性

  • 相关阅读:
    5.11-上位机重新编程
    3.30-计算机系统互联方案
    3.25-两个操作者的通信模式
    3.23-重新定义操作者框架
    go 修改数组中对象的值不生效的解决方法
    go orm QueryTable Filter 不生效解决方法
    Beego orm.Install() 插入 [单条记录] 或 [一批记录],及出现异常 Handler crashed with error <Ormer> table: `.` not found, make sure it was registered with `RegisterModel()`
    go json 序列号、反序列号和数据类型转换
    go json 转换忽略字段、控制字段可有可无
    Flutter 使用 flutter_inappbrowser 加载 H5 及与 js 交互,Methods marked with @UiThread must be executed on the main thread . Current thread: JavaBridge
  • 原文地址:https://www.cnblogs.com/qingcong/p/5918986.html
Copyright © 2011-2022 走看看