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

转载于:https://www.cnblogs.com/Nyan-Workflow-FC/p/4757942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值