类对自己本身的引用可以吗?答案是肯定的。
<<Hibernate in Action>>里面有个很好的例子。注意看下面的代码,Category表示一个商品的类别,他有一个parent Category,也有很多Children Categories,想一想类似文件结构的概念。
package bidshop.bean;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class Category implements Serializable
{
private Long id; // the unique database id.
// Whenever an association is created between a parent Category and a child
// Category, two actions are required.
//
// Category aPrent = new Category();
// Category achild = new Category();
//
// 1. The parentCategory of the child must be set, effectively breaking
// the association between the child and its own parent. (only one parent)
//
// aCHild.setParentCategory(aParent);
//
// 2. The child must be added to the childCategories collection of the parent
//
// aParent.getChildCategories().add(aChild);
private String name;
private Category parentCategory;
// Hibernate requires interfaces for collection-type attributes, so must use
// java.util.Set or java.util.List rather than HashSet.
private Set<Category> childCategories; // set because duplicates are disallowed.
private Set<Item> items;
/**
* default constructor with no args.
*/
public Category()
{
childCategories = new HashSet<Category>();
items = new HashSet<Item>();
}
public Long getId()
{
return id;
}
/**
* Hibernate will generate and set the identifier value.
* Only exception is classes with *natrual keus* (where the value of the id
* is assigned by the application before the object is made persistent)
* Hibernate does not allow you to change the identifier value of a persistent
* instance after it's first assigned.
* @param id unique data identifier
*/
private void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Category getParentCategory()
{
return parentCategory;
}
public void setParentCategory(Category parentCategory)
{
this.parentCategory = parentCategory;
}
public Set getChildCategories()
{
// The clien will get an excetion if she tries to modify the collection
// Every modification is forced to go through the relationship-mgt method.
return Collections.unmodifiableSet(childCategories);
}
/**
* Using addChildCategory() instead.
* Hibernate doesn't care if property accessor methods are private or public.
*
* @param childCategories childCategories to be added.
*/
private void setChildCategories(Set<Category> childCategories)
{
this.childCategories = childCategories;
}
public Set<Item> getItems()
{
return items;
}
public void setItems(Set<Item> items)
{
this.items = items;
}
/**
* Enforce the cardinality of the association.
* Errors that arise from leaving out one of the two required
* actions are avoided.
*/
public void addChildCategory(Category childCategory)
{
if (childCategory == null)
{
throw new IllegalArgumentException("Null child category!");
}
if (childCategory.getParentCategory() != null)
{
childCategory.getParentCategory().getChildCategories().remove(childCategory);
}
childCategory.setParentCategory(this);
childCategories.add(childCategory);
}
}
注意this这个关键字,不要混淆了。