• hibernate ORM related


    一、单向关联(unidirectional associations):

    1.1.1 Many-to-one

    Employee.hbm.xml
    <class name="Employee" table="T_EMPLOYEE">
        <id name="employee_Id" column="EMPLOYEE_ID">
            <generator class="native"/>
        </id>
        <many-to-one name="department" column="DEPARTMENT_ID" not-null="true"/>
    </class>
    Department.hbm.xml
    <class name="Department" table="T_DEPARTMENT"> <id name="department_Id" column="DEPARTMENT_ID"> <generator class="native"/> </id> </class> create table EMPLOYEE(employee_Id bigint not null primary key,department_Id bigint not null) create table DEPARTMENT(department_Id bigint not null primary key)

    ?单向的<many-to-one>里没有指明class

    1.1.2 One-t-one

    1>单向一对一和单向多对一区别就是Unique这个属性,基于外键

    <class name="Employee" table="T_EMPLOYEE">
        <id name="employee_Id" column="EMPLOYEE_ID">
            <generator class="native"/>
        </id>
        <many-to-one name="department"
                               column="DEPARTMENT_ID" 
                               unique="true"
                               not-null="true"/>
    </class>
    
    <class name="Department" table="T_DEPARTMENT">
        <id name="department_Id" column="DEPARTMENT_ID">
            <generator class="native"/>
        </id>
    </class>    

    2>基于主键

    <class name="Person" table="Person">
        <id name="Person_Id" column="PERSON_ID">
            <generator class="native"/>
        </id>
    </class>
    
    <class name="Card" table="Card">
        <id name="Card_Id" column="PERSON_ID">
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        <one-to-one name="person" constrained="true"/>
    </class>
    
    create table Person ( PERSON_ID  bigint not null primary key )
    create table Card( PERSON_ID bigint not null primary key )

    ? one-to-one 没有column和table属性,有class属性,name标志?,如何指定person对象。

    1.1.3 One-to-many

    <class name="Department">
        <id name="department_Id" column="DEPARTMENT_ID">
            <generator class="native"/>
        </id>
        <set name="employee">
            <key column="DEPARTMENT_ID" 
                not-null="true"/>
            <one-to-many class="Employee"/>
        </set>
    </class>
    
    <class name="Employee">
        <id name="employee_Id" column="EMPLOYEE_ID">
            <generator class="native"/>
        </id>
    </class>
    
    create table Department( department_Id bigint not null primary key )
    create table Emplyee( employee_Id bigint not null primary key, personId bigint not null )

    you should instead use a join table for this kind of asociation

    二、单向关联with join table

    2.1.1 One-to-many

    <class name="Department">
        <id name="department_Id" column="DEPARTMENT_ID">
            <generator class="native"/>
        </id>
        <set name="employees" table="DepartmetEmployee">
            <key column="DEPARTMENT_ID"/>
            <many-to-many column="EMPLOYEE_ID"
                unique="true"
                class="Employee"/>
        </set>
    </class>
    
    <class name="Employee">
        <id name="employee_Id" column="EMPLOYEE_ID">
            <generator class="native"/>
        </id>
    </class>
    
    create table Department( Department_Id bigint not null primary key )
    create table DepartmentEmployee(Department_Id bigint not null, employee_Id bigint not null primary key ) 
    create table Employee( employee_Id bigint not null primary key )

    specifying unique="ture" change multiplicity from many-to-many to one-to-many.

    2.1.2.Many-to-one

    <class name="Department">
        <id name="department_Id" column="DEPARTMENT_ID">
            <generator class="native"/>
        </id>
    </class>
    
    <class name="Employee">
        <id name="employee_Id" column="EMPLOYEE_ID">
            <generator class="native"/>
        </id>
        <join table="EmployeeDepartment" 
            optional="true">
            <key column="EMPLOYEE_ID" unique="true"/>
            <many-to-one name="department"
                column="DEPARTMENT_ID" 
                not-null="true"/>
        </join>
    </class>

    2.1.3 One-to-one

    <class name="Person" table="Person">
        <id name="Person_Id" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <join table="PersonCard" 
            optional="true">
            <key column="PERSON_ID" 
                unique="true"/>
            <many-to-one name="card"
                column="CARD_ID" 
                not-null="true"
                unique="true"/>
           </join>
    </class>
    
    <class name="Card" table="Card">
        <id name="Card_Id" column="CARD_ID">
            <generator class="native"/>
        </id>
        <one-to-one name="person" constrained="true"/>
    </class>
    
    create table Person ( PERSON_ID  bigint not null primary key )
    create table PersonCard ( PERSON_ID bigint not null primary key, CARD_ID bigint not null unique )
    create table Card( CARD_ID bigint not null primary key )

    2.1.4.Many-to-many

    <class name="Person">
        <id name="id" column="personId">
            <generator class="native"/>
        </id>
        <set name="addresses" table="PersonAddress">
            <key column="personId"/>
            <many-to-many column="addressId"
                class="Address"/>
        </set>
    </class>
    
    <class name="Address">
        <id name="id" column="addressId">
            <generator class="native"/>
        </id>
    </class>
    
    create table Person ( personId bigint not null primary key )
    create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
    create table Address ( addressId bigint not null primary key )

    三、双向关联(Bidirectional associations)

    3.1.1.one-to-one

    1>

    <class name="Person">
        <id name="id" column="personId">
            <generator class="native"/>
        </id>
        <one-to-one name="address"/>
    </class>
    
    <class name="Address">
        <id name="id" column="personId">
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        <one-to-one name="person" 
            constrained="true"/>
    </class>
    create table Person ( personId bigint not null primary key )
    create table Address ( personId bigint not null primary key )
    
    

    2>

    <class name="Person">
        <id name="id" column="personId">
            <generator class="native"/>
        </id>
        <many-to-one name="address" 
            column="addressId" 
            unique="true"
            not-null="true"/>
    </class>
    
    <class name="Address">
        <id name="id" column="addressId">
            <generator class="native"/>
        </id>
       <one-to-one name="person" 
            property-ref="address"/>
    </class>
    
    create table Person ( personId bigint not null primary key, addressId bigint not null unique )
    create table Address ( addressId bigint not null primary key )

    3.1.2.one-to-many/many-to-one:

    1>

    <class name="Person">
        <id name="id" column="personId">
            <generator class="native"/>
        </id>
        <many-to-one name="address" 
            column="addressId"
            not-null="true"/>
    </class>
    
    <class name="Address">
        <id name="id" column="addressId">
            <generator class="native"/>
        </id>
        <set name="people" inverse="true">
            <key column="addressId"/>
            <one-to-many class="Person"/>
        </set>
    </class>
    
    create table Person ( personId bigint not null primary key, addressId bigint not null )
    create table Address ( addressId bigint not null primary key )

    2>

    <class name="Person">
       <id name="id"/>
       ...
       <many-to-one name="address"
          column="addressId"
          not-null="true"
          insert="false"
          update="false"/>
    </class>
    
    <class name="Address">
       <id name="id"/>
       ...
       <list name="people">
          <key column="addressId" not-null="true"/>
          <list-index column="peopleIdx"/>
          <one-to-many class="Person"/>
       </list>
    </class>

     四、双向关联with join table

    4.1.1.one-to-many/many-to-one

    4.1.2.one-to-one

    4.1.3.many-to-many

  • 相关阅读:
    2019-2020-2 网络对抗技术 20175213吕正宏 Exp4 恶意代码分析
    2019-2020-2 网络对抗技术 20175213吕正宏 Exp3 免杀原理与实践
    2019-2020-2 网络对抗技术 20175213吕正宏 Exp2 后门原理与实践
    2019-2020-2 网络对抗技术 20175213吕正宏 Exp1 PC平台逆向破解
    ucosii-2(选做)
    2019-2020-2 网络对抗技术 20175224 Exp9 Web安全基础
    2019-2020-2 网络对抗技术 20175224 Exp8 Web基础
    2019-2020-2 网络对抗技术 20175224 Exp7 网络欺诈防范
    2019-2020-2 网络对抗技术 20175224 Exp6 MSF基础应用
    2019-2020-2 网络对抗技术 20175224 Exp5 信息搜集与漏洞扫描
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/4757942.html
Copyright © 2020-2023  润新知