top
Loading...
使用hibernate實現樹形結構無限級分類

推薦:更多Hiernate開發資料

在系統中,經常會用到無限級的樹形結構分類,如組織機構管理、商品/地區分類等等。一般無外采用兩種方式:

一是類似struts-menu(http://struts-menu.sourceforge.net)的XML文件管理方式,配置起來比較方便,但很難與系統中其它應用數據集成;

二是使用數據庫存儲,定義父子關系。

在我們現在開發的一個產品中,使用hibernate實現了一套樹形結構的處理方法,簡介如下:

演示地址:http://219.143.69.2:8000/treetest/menumanage.do?todoaction=list

演示的是系統菜單的層次實現。由于菜單本身屬于權限系統的一部分,存儲在數據庫中后可以方便的與部門、用戶、崗位、職務等進行關聯,并進行權限控制。

樹形結構顯示,使用的是xtree。為便于編輯維護,自己寫了一個左鍵彈出菜單(xtree的右鍵事件無法更改),進行節點的添加、修改、刪除、轉移操作。(PS:這套維護界面是完全跨瀏覽器的,有興趣的不妨一試)

關聯關系

可以使用objects對象來配置關聯關系,實現多對多/一對多等關系。在BaseTree中,getObjects()方法是abstract的,可以根據需要自己定義。如論壇分類與每個分類所對應的貼子相關聯,商品分類與商品編碼相關聯等,可以根據需要來處理hbm文件。若需要多項關聯,亦可擴展。如菜單與用戶、部門、崗位分別進行關聯

hibernate2.1.7的一個bug,在這個測試源碼的dao中,TreeManager的getRoots方法,
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。 

主要代碼

繼承關系如下,假如要實現國家分類:

CountryTree extends BaseTree(abstract class)
BaseTree(abstract class) implements Tree(interface)

為節省版面,下面代碼去掉了javadoc

Tree.java 代碼

  1. /**  
  2.  * 實現了樹的基本操作,上溯、下溯、子節點的添加/移除和遞歸查找、對象關聯等  
  3.  */  
  4. package test.testtree.base;   
  5. import java.util.Set;   
  6.   
  7. public interface Tree {    
  8.     public String getCode();   
  9.     public String getName();   
  10.     public String getDescription();   
  11.     public Tree getParent();   
  12.     public boolean isRoot();   
  13.     public boolean isLeaf();   
  14.     public boolean isParentOf(Tree tree);   
  15.     public boolean isChildOf(Tree tree);   
  16.     public void addChild(Tree tree);   
  17.     public void rmChild(Tree tree);   
  18.     public Set getAllChildren();   
  19.     public Set getChildren();   
  20.     public Set getAllLeaves();   
  21.     public void addObject(Object obj);   
  22.     public void rmObject(Object obj);   
  23.     public Set getObjects();   
  24.     public Long getId();   
  25. }   

BaseTree.java代碼

  1. package test.testtree.base;   
  2. import java.util.*;   
  3.   
  4. public abstract class BaseTree extends BasePojo implements Tree{       
  5.     protected String code;       
  6.     protected String name;       
  7.     protected String description;           
  8.     protected BaseTree parent;      
  9.     protected Set children = new HashSet();       
  10.     protected Set objects = new HashSet();       
  11.     public void setCode(String code) {   
  12.         this.code = code;   
  13.     }       
  14.     abstract public String getCode();   
  15.     public void setName(String name) {   
  16.         this.name = name;   
  17.     }       
  18.     abstract public String getName();       
  19.     public void setDescription(String description) {   
  20.         this.description = description;   
  21.     }   
  22.     abstract public String getDescription();   
  23.     abstract public Tree getParent();           
  24.     public boolean isRoot() {   
  25.         return (getParent()==null);   
  26.     }       
  27.     public boolean isLeaf() {   
  28.         return (this.getChildren().size()==0);   
  29.     }       
  30.     public boolean isParentOf(Tree tree) {   
  31.         if (tree==null || ((BaseTree) tree).equals(this)) {   
  32.             /*如果對方為空*/  
  33.             return false;   
  34.         }else if(this.isLeaf()){   
  35.             /*如果自己為葉子,則返回FALSE*/  
  36.             return false;   
  37.         }else if(tree.isRoot()){   
  38.             /*如果對方為根,返回FALSE*/  
  39.             return false;   
  40.         }else{   
  41.             BaseTree bt = (BaseTree) (tree.getParent());   
  42.             if (this.equals(bt)){   
  43.                 /*如果對方的父節點是自己,則返回TRUE*/  
  44.                 return true;   
  45.             }else{   
  46.                 /*判斷對方的父節點是否是自己的孩子,進行遞歸*/  
  47.                 return isParentOf(bt);   
  48.             }   
  49.         }   
  50.     }   
  51.     public boolean isChildOf(Tree tree) {   
  52.         return (tree.isParentOf(this));   
  53.     }   
  54.     public void addChild(Tree tree) {   
  55.         children.add(tree);   
  56.     }   
  57.     public void rmChild(Tree tree) {   
  58.         children.remove(tree);   
  59.         ((BaseTree) tree).setParent(null);   
  60.     }   
  61.     public Set getAllLeaves() {   
  62.         Set set_old = this.getAllChildren();   
  63.         Set set = new HashSet();   
  64.         set.addAll(set_old);   
  65.         Iterator itr = set_old.iterator();   
  66.         while(itr.hasNext()){   
  67.             BaseTree bt = (BaseTree) itr.next();   
  68.             if (! bt.isLeaf()){   
  69.                 set.remove(bt);   
  70.             }   
  71.         }   
  72.         return set;   
  73.     }           
  74.     public Set getAllChildren() {   
  75.         Set set = new HashSet();   
  76.         Stack stack = new Stack();   
  77.         stack.push(this);   
  78.         while(!stack.empty()){   
  79.             BaseTree bt = (BaseTree) stack.pop();   
  80.             set.add(bt);   
  81.             Iterator itr = bt.getChildren().iterator();   
  82.             while(itr.hasNext()){   
  83.                 BaseTree btchild = (BaseTree) itr.next();   
  84.                 stack.push(btchild);   
  85.             }   
  86.         }   
  87.         set.remove(this);   
  88.         return set;   
  89.     }       
  90.     public List getMeAndListAllChildren() {           
  91.         List lst = new Vector();   
  92.         lst.add(this);   
  93.         Iterator itr = this.getChildren().iterator();   
  94.         while(itr.hasNext()){   
  95.             BaseTree bt = (BaseTree) itr.next();   
  96.             lst.addAll(bt.getMeAndListAllChildren());                  
  97.         }           
  98.         return lst;   
  99.     }   
  100.     abstract public Set getChildren();   
  101.     public void addObject(Object obj) {   
  102.         objects.add(obj);   
  103.     }   
  104.     public void rmObject(Object obj) {   
  105.         objects.remove(obj);   
  106.     }   
  107.     abstract public Set getObjects();   
  108.     public void setParent(Tree parent) {   
  109.         this.parent = (BaseTree) parent;   
  110.     }   
  111.     public void setChildren(Set children) {   
  112.         this.children = children;   
  113.     }   
  114.     public void setObjects(Set objects) {   
  115.         this.objects = objects;   
  116.     }       
  117. }   
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗