在我们现在开发的一个产品中,使用hibernate实现了一套树形结构的处理方法,简介如下:
session.createQuery(" from " + cls.getName() + " where enabled=? and parent_id is null order by id");
在hibernate2中必须像写成parent_id is null,才能正确运行,这应该是2.1.7中的一个bug。而hibernate3中,可以使用parent is null的hsql。
BaseTree(abstract class) implements Tree(interface)
/**
* 实现了树的基本操作,上溯、下溯、子节点的添加/移除和递归查找、对象关联等
*/
package test.testtree.base;;
import java.util.Set;;
public interface Tree {;;;
public String getCode();;
public String getName();;
public String getDescription();;
public Tree getParent();;
public boolean isRoot();;
public boolean isLeaf();;
public boolean isParentOf(Tree tree);;
public boolean isChildOf(Tree tree);;
public void addChild(Tree tree);;
public void rmChild(Tree tree);;
public Set getAllChildren();;
public Set getChildren();;
public Set getAllLeaves();;
public void addObject(Object obj);;
public void rmObject(Object obj);;
public Set getObjects();;
public Long getId();;
};
BaseTree.java代码
package test.testtree.base;;
import java.util.*;;
public abstract class BaseTree extends BasePojo implements Tree{
protected String code
protected String name
protected String description;;
protected BaseTree parent;;;;;
protected Set children = new HashSet()
protected Set objects = new HashSet()
public void setCode(String code) {;
this.code = code;;
};;;;;
abstract public String getCode();;
public void setName(String name) {;
this.name = name;;
};;;;;
abstract public String getName()
public void setDescription(String description) {;
this.description = description;;
};
abstract public String getDescription();;
abstract public Tree getParent();;
public boolean isRoot() {;
return (getParent()==null);;
};;;;;
public boolean isLeaf() {;
return (this.getChildren().size()==0);;
};;;;;
public boolean isParentOf(Tree tree) {;
if (tree==null || ((BaseTree) tree).equals(this)) {;
/*如果对方为空*/
return false;;
}else if(this.isLeaf()){;
/*如果自己为叶子,则返回FALSE*/
return false;;
}else if(tree.isRoot()){;
/*如果对方为根,返回FALSE*/
return false;;
}else{;
BaseTree bt = (BaseTree) (tree.getParent());;
if (this.equals(bt)){;
/*如果对方的父节点是自己,则返回TRUE*/
return true;;
}else{;
/*判断对方的父节点是否是自己的孩子,进行递归*/
return isParentOf(bt);;
};
};
};
public boolean isChildOf(Tree tree) {;
return (tree.isParentOf(this));;
};
public void addChild(Tree tree) {;
children.add(tree);;
};
public void rmChild(Tree tree) {;
children.remove(tree);;
((BaseTree) tree).setParent(null);;
};
public Set getAllLeaves() {;
Set set_old = this.getAllChildren();;
Set set = new HashSet();;
set.addAll(set_old);;
Iterator itr = set_old.iterator();;
while(itr.hasNext()){;
BaseTree bt = (BaseTree) itr.next();;
if (! bt.isLeaf()){;
set.remove(bt);;
};
};
return set;;
};
public Set getAllChildren() {;
Set set = new HashSet();;
Stack stack = new Stack();;
stack.push(this);;
while(!stack.empty()){;
BaseTree bt = (BaseTree) stack.pop();;
set.add(bt);;
Iterator itr = bt.getChildren().iterator();;
while(itr.hasNext()){;
BaseTree btchild = (BaseTree) itr.next();;
stack.push(btchild);;
};
};
set.remove(this);;
return set;;
};;;;;
public List getMeAndListAllChildren() {;;
List lst = new Vector();;
lst.add(this);;
Iterator itr = this.getChildren().iterator();;
while(itr.hasNext()){;
BaseTree bt = (BaseTree) itr.next();;
lst.addAll(bt.getMeAndListAllChildren());
};
return lst;;
};
abstract public Set getChildren();;
public void addObject(Object obj) {;
objects.add(obj);;
};
public void rmObject(Object obj) {;
objects.remove(obj);;
};
abstract public Set getObjects();;
public void setParent(Tree parent) {;
this.parent = (BaseTree) parent;;
};
public void setChildren(Set children) {;
this.children = children;;
};
public void setObjects(Set objects) {;
this.objects = objects;;
};;;;;
}
标签: