inverse:

example:

<class
    name="Item"
    table="ITEM">
    ...
    <set name="bids"
         inverse="true">

        <key column="ITEM_ID"/>
        <one-to-many class="Bid"/>
    </set>
</class>

 

It’s  a  hint  that  tells  Hibernate  you  mapped  the  same  foreign  key
column twice. 
If you only call anItem.getBids().add(bid), no changes are made persistent!
You get what you want only if the other side, aBid.setItem(anItem), is set correctly.

==============================

cascade:

cascading is used as a convenience feature.

下面这个例子有两个session.save

Item newItem = new Item(x);
Bid newBid = new Bid();
newItem.addBid(newBid); // Set both sides of the association
session.save(newItem);
session.save(newBid);

可以用cascading简化:

<class
    name="Item"
    table="ITEM">
    ...
    <set name="bids"
         inverse="true"
        cascade="save-update">
        <key column="ITEM_ID"/>
        <one-to-many class="Bid"/>
    </set>
</class>

第七章:

7.1 Single-valued entity associations

7.1.1 shared primary key association:

user class:

<one-to-one name="shippingAddress"
            class="Address"
            cascade="save-update"/>

address class:

<class name="Address" table="ADDRESS">
    <id name="id" column="ADDRESS_ID">
        <generator class="foreign">
            <param name="property">user</param>
        </generator>
    </id>

    ...
    <one-to-one name="user"
                class="User"
                constrained="true"/>
</class>

 

This  mapping  not  only  makes  the  association  bidirectional,  but  also,  with  constrained="true",  adds  a  foreign  key  constraint  linking  the  primary  key  of  the ADDRESS table to the primary key of the USERS table.

 

7.1.2 One-to-one foreign key association

<class name="User" table="USERS">
    <many-to-one name="shippingAddress"
                 class="Address"
                 column="SHIPPING_ADDRESS_ID"
                 cascade="save-update"
                 unique="true"/>
</class>

解释为什么是many-to-one 而不是one-to-one:

The mapping element in XML for this association is <many-to-one>—not <one-
to-one>, as you might have expected. The reason is simple: You don’t care what’s
on the target side of the association, so you can treat it like a to-one association
without the many part.

 

<one-to-one name="user"
            class="User"
            property-ref="shippingAddress"/>
You tell Hibernate that the user property of the Address class is the inverse of a property   on   the   other   side   of   the   association. 

 

7.1.3 Mapping with a join table:

这往往是用在optional link的case上,比如Person and Desk的例子。下面这个例子假定: A shipment has an optional link with a single auction item.

image

public class Shipment {
    ...
    private Item auction;
    ...
    // Getter/setter methods
}

<class name="Shipment" table="SHIPMENT">
    <id name="id" column="SHIPMENT_ID">...</id>
    ...
    <join table="ITEM_SHIPMENT" optional="true">
        <key column="SHIPMENT_ID"/>
        <many-to-one name="auction"
                     column="ITEM_ID"
                     not-null="true"
                     unique="true"/>
    </join>
</class>

note:again, 因为是map到foreign key column, 所以用many-to-one, 跟7.1.2的笔记一样

You could map this association bidirectional, with the same technique on the
other side. However, optional one-to-one associations are unidirectional most of
the time.

7.2 Many-valued entity associations

7.2.1 One-to-many associations

considering bags:

Bags have the most efficient performance characteristics of all the collections
you can use for a bidirectional one-to-many entity association

Because a bag doesn’t have to maintain the index of its elements (like a list) or check for duplicate elements (like a set)

 

Unidirectional and bidirectional lists:

对于List来说,因为有list-index.这个时候如果用普通的inverse=true放在list上,那么在collection的一端(比如bid)上来控制association, 那么list-index没法有值,所以需要在<many-to-one>上做inverse=true. 而<many-to-one>上是没有inverse属性的,所以这样模拟:

<class name="Bid"
       table="BID">
    ...
    <many-to-one name="item"
                 column="ITEM_ID"
                 class="Item"
                 not-null="true"
                insert="false"
                 update="false"/
>
</class>

这个通常表示read-only

 

Optional one-To-Many association with a join table:

image

user:

<set name="boughtItems" table="ITEM_BUYER">
    <key column="USER_ID"/>
    <many-to-many class="Item"
                  column="ITEM_ID"
                  unique="true"/>
</set>

 

Item:

<join table="ITEM_BUYER"
      optional="true"
     inverse="true">
     <key column="ITEM_ID” unique=”true” not-null="true"/>
     <many-to-one name="buyer" column="USER_ID"/>
</join>