一、one-many ,需要一個有序的list. 建議影射方式 :

private List _items;

<bag
name="items"
inverse="true"   //盡量使用雙向關聯
order-by="DATE_TIME"
cascade="all">
<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>
</bag>


many-to-many ,建議用 set



二、one-to-one 適用
            通過主鍵進行關聯
            相當于把大表拆分為多個小表
            例如把大字段單獨拆分出來,以提高數據庫操作的性能

三、composite element ,必須依賴的導航關系

 <list name="lineItems" table="line_items">
<key column="order_id"/>
<list-index column="line_number"/>
<composite-element class="LineItem">
<property name="quantity"/>
<many-to-one name="product" column="product_id"/>
</composite-element>
</list>

四、 one-one formula , 很復雜,有點不明白

 <class name="Person">
<id name="name"/>
<one-to-one name="address"
cascade="all">
<formula>name</formula>
<formula>'HOME'</formula>
</one-to-one>
<one-to-one name="mailingAddress"
cascade="all">
<formula>name</formula>
<formula>'MAILING'</formula>
</one-to-one>
</class>
<class name="Address" batch-size="2"
check="addressType in ('MAILING', 'HOME', 'BUSINESS')">
<composite-id>
<key-many-to-one name="person"
column="personName"/>
<key-property name="type"
column="addressType"/>
</composite-id>
<property name="street" type="text"/>
<property name="state"/>
<property name="zip"/>
</class>


五、繼承關系, per subclass table ,no discriminator ,joined-subclass




六、tree
拷貝: http://www.thogau.net/tutorials/tree/tutorial02-01.jsp


package net.thogau.website.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * This class implements a persisted tree node.
 *
 * @author <a href="mailto:thogau@thogau.net">thogau</a>
 *
 * @struts.form include-all="false" extends="BaseForm"
 * @hibernate.class table="node"
 */
public class Node extends BaseObject implements Serializable {
   
    // mapped to primary key in node table
    protected Long id;
       
    protected String name;
   
    protected Node parent = null;
   
    protected List children = new ArrayList();
   
    /**
     * @hibernate.id column="id" generator-class="native" unsaved-value="null"
     * @struts.form-field
     */
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }   
   
    /**
     * Returns the node name.
     *
     * @return String
     *
     * @hibernate.property column="name" not-null="true" unique="true"
     * @struts.form-field
     * @struts.validator type="required"
     *
     */
    public String getName() {
        return name;
    }  
    public void setName(String name) {
        this.name = name;
    }
   
    /**
     * Returns the node's children.
     *
     * @return List
     *
     * @hibernate.list cascade="all-delete-orphan" inverse="true"
     * @hibernate.collection-one-to-many class="net.thogau.website.model.Node"
     * @hibernate.collection-index column="position"
     * @hibernate.collection-key column="parent_id"
     * @struts.form-field
     */
    public List getChildren() {
        return children;
    }

    public void setChildren(List children) {
        this.children = children;
    }
   
    /**
     * Returns the position of the node in the children list (if it has parent).
     * @return int
     *
     * @hibernate.property column="position"
     */   
    public int getPosition() {
        try{
            return parent.getChildren().indexOf(this);
        }
        catch(NullPointerException e){
            // if it has no parent, position makes no sense
            return -1;
        }
    }
   
    public void setPosition(int position) { /* not used */ }
   
    /**
     * Returns the node's parent.
     *
     * @return Node
     *
     * @hibernate.many-to-one column = "parent_id" class="net.thogau.website.model.Node" cascade = "none"
     * @hibernate.column name="parent_id"
     */
     public Node getParent() {
        return parent;
    }
    
    public void setParent(Node n) {
        this.parent = n;
    }
   
    /**
     * @see java.lang.Object#equals(Object)
     */
    public boolean equals(Object object) {
        if (!(object instanceof Node)) {
            return false;
        }
        Node rhs = (Node) object;
        return new EqualsBuilder().append(this.name, rhs.name).append(
                this.children, rhs.children).append(this.parent, rhs.parent)
                .append(this.id, rhs.id).isEquals();
    }
   
    /**
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return new HashCodeBuilder(1036586079, -537109207).append(this.name)
                .append(this.parent.getName()).append(this.id)
                .toHashCode();
    }
   
    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("name", this.name).append("parent", this.parent)
                .append("id", this.id).append("position", this.getPosition()).toString();
    }
   
}

好像,equal ,hash 是必須的。

# /**
#      * 樹形遍歷
#      * 不用遞歸,用堆棧.
#      * 這里只是做為例子,本人不建議把業務邏輯封裝在Entity層.
#                 */ 
#     public List getVisitResults() { 
#         List l = new ArrayList(); 
#         Stack s = new Stack(); 
#         s.push(this); 
#         while (s.empty() == false) { 
#            Cat c = (Cat) s.pop(); 
#            l.add(c); 
#            List children = c.getChildren(); 
#               if (children != null) { 
#                  for (int i = 0; i <  hildren.size(); i++)   { 
#             Cat cat = (Cat) children.get(i); 
#             s.push(cat); 
#                  }//end for 
#              }//end if 
#         }//end while 
#         return l; 
#     } 






 









西津渡