日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Hibernate樹形結(jié)構(gòu)

本文向大家介紹Hibernate樹形結(jié)構(gòu),可能好多人還不了解Hibernate樹形結(jié)構(gòu),沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

創(chuàng)新互聯(lián)建站專業(yè)提供西部信息中心服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買西部信息中心服務(wù),并享受7*24小時金牌售后服務(wù)。

在系統(tǒng)中,經(jīng)常會用到無限級的Hibernate樹形結(jié)構(gòu)分類,如組織機(jī)構(gòu)管理、商品/地區(qū)分類等等。一般無外采用兩種方式:
◆一是類似struts-menu的XML文件管理方式,配置起來比較方便,但很難與系統(tǒng)中其它應(yīng)用數(shù)據(jù)集成;
◆二是使用數(shù)據(jù)庫存儲,定義父子關(guān)系。

在我們現(xiàn)在開發(fā)的一個產(chǎn)品中,實現(xiàn)了一套Hibernate樹形結(jié)構(gòu)的處理方法,簡介如下:

一.Hibernate樹形結(jié)構(gòu)顯示

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

二.關(guān)聯(lián)關(guān)系:

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

三.主要代碼:

 
 
 
  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. /*如果對方的父節(jié)點是自己,則返回TRUE*/  
  44. return true;  
  45. }else{  
  46. /*判斷對方的父節(jié)點是否是自己的孩子,進(jìn)行遞歸*/  
  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. }  

【編輯推薦】

  1. 描述Hibernate持久性類
  2. 分析Java應(yīng)用程序和Hibernate
  3. Hibernate3和JBOSS 3.2強(qiáng)強(qiáng)聯(lián)手
  4. 詳解Hibernate.properties文件
  5. 淺談定制Hibernate映射

分享標(biāo)題:詳解Hibernate樹形結(jié)構(gòu)
本文來源:http://www.dlmjj.cn/article/dhisjso.html