一、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;
# }
西津渡