自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Hibernate樹形結構

開發(fā) 后端
這里介紹在系統(tǒng)中,經(jīng)常會用到無限級的Hibernate樹形結構分類,如組織機構管理、商品/地區(qū)分類等等。一般無外采用兩種方式。

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

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

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

一.Hibernate樹形結構顯示

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

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

可以使用objects對象來配置關聯(lián)關系,實現(xiàn)多對多/一對多等關系。在BaseTree中,getObjects()方法是abstract的,可以根據(jù)需要自己定義。如論壇分類與每個分類所對應的貼子相關聯(lián),商品分類與商品編碼相關聯(lián)等,可以根據(jù)需要來處理hbm文件。若需要多項關聯(liá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é)點是否是自己的孩子,進行遞歸*/  
  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應用程序和Hibernate
  3. Hibernate3和JBOSS 3.2強強聯(lián)手
  4. 詳解Hibernate.properties文件
  5. 淺談定制Hibernate映射
責任編輯:佚名 來源: IT168
相關推薦

2009-06-03 09:11:03

Hibernate工作原理體系結構

2009-09-21 17:33:50

Hibernate基礎

2009-09-24 17:24:20

Hibernate S

2009-09-24 15:53:00

Hibernate J

2009-06-02 14:46:26

Hibernate關系映射教程

2024-09-19 08:22:41

2009-09-27 10:28:12

Hibernate.p

2009-07-09 16:01:27

2009-09-23 16:30:01

Hibernate f

2009-09-22 17:25:41

優(yōu)化Hibernate

2009-09-24 13:03:38

Hibernate C

2009-07-28 13:48:28

ASP.NET樹形圖

2009-09-23 12:48:54

Hibernate I

2012-02-14 15:51:13

JavaHibernate

2009-09-25 10:38:42

Hibernate動態(tài)

2010-05-24 19:17:12

SNMP對象

2017-02-08 14:16:17

C代碼終端

2015-04-27 09:50:45

Java Hibern連接池詳解

2011-04-07 11:06:18

Hibernate

2010-11-15 11:58:02

Oracle物理結構
點贊
收藏

51CTO技術棧公眾號