Use hibernate to implement unlimited CLASSIFICATION OF TREE STRUCTURES

Source: Internet
Author: User
Tags addall addchild set set
Use hibernate to implement unlimited CLASSIFICATION OF TREE STRUCTURES
 
In the system, infinite tree structure classification is often used, such as Organization Management and Product/Region Classification. Generally, the following two methods are used:

One is similar to Struts-menu (http://struts-menu.sourceforge.net) XML file management method, configuration is more convenient, but it is difficult to integrate with other applications in the system;

The second is to use database storage to define parent-child relationships.

In a product we are developing, Hibernate is used to implement a set of tree structure processing methods. The introduction is as follows:

The menu itself is part of the permission system. After it is stored in the database, it can be easily associated with departments, users, positions, and positions for permission control.

Tree Structure displayThe xtree is used. To facilitate editing and maintenance, you have written a left-click pop-up menu (the right-click event of xtree cannot be changed) to add, modify, delete, and transfer nodes. (PS: This maintenance interface is completely cross-browser. If you are interested, try it)

Association:

You can use objects to configure associations to implement many-to-many/one-to-many relationships. In basetree, The getobjects () method is abstract and can be defined as needed. For example, the Forum category is associated with the posts corresponding to each category, and the commodity category is associated with the commodity code. HBM files can be processed as needed. Multiple associations can also be expanded. For example, menus are associated with users, departments, and positions.

A bug in hibernate2.1.7In the DAO of the test source code, the treemanager getroots method,
Session. createquery ("from" + Cls. getname () + "where enabled =? AndParent_id is nullOrder by ID ");
In hibernate2, the image must be writtenParent_id is nullTo run correctly. This should be a bug in 2.1.7. In hibernate3, hsql with parent is null can be used.

Main Code:

The inheritance relationship is as follows:

Countrytree extends basetree (abstract class)
Basetree (abstract class) implements tree (Interface)

To save layout, the following code removes javadoc

Tree. Java code

  1. /**
  2. * The basic operations of the tree are implemented, including ascending, descending, subnode addition/removal, recursive search, and object Association.
  3. */
  4. Package test. testtree. base;
  5. Import java. util. Set;
  6. Public interface tree {
  7. Public String getcode ();
  8. Public String getname ();
  9. Public String getdescription ();
  10. Public tree getparent ();
  11. Public Boolean isroot ();
  12. Public Boolean isleaf ();
  13. Public Boolean isparentof (TREE tree );
  14. Public Boolean ischildof (TREE tree );
  15. Public void addchild (TREE tree );
  16. Public void rmchild (TREE tree );
  17. Public set getallchildren ();
  18. Public set getchildren ();
  19. Public set getallleaves ();
  20. Public void addobject (Object OBJ );
  21. Public void rmobject (Object OBJ );
  22. Public set getobjects ();
  23. Public long GETID ();
  24. }

Basetree. Java code

  1. Package test. testtree. base;
  2. Import java. util .*;
  3. Public abstract class basetree extends basepojo implements tree {
  4. Protected string code;
  5. Protected string name;
  6. Protected string description;
  7. Protected basetree parent;
  8. Protected set children = new hashset ();
  9. Protected set objects = new hashset ();
  10. Public void setcode (string code ){
  11. This. Code = code;
  12. }
  13. Abstract Public String getcode ();
  14. Public void setname (string name ){
  15. This. Name = Name;
  16. }
  17. Abstract Public String getname ();
  18. Public void setdescription (string description ){
  19. This. Description = description;
  20. }
  21. Abstract Public String getdescription ();
  22. Abstract Public tree getparent ();
  23. Public Boolean isroot (){
  24. Return (getparent () = NULL );
  25. }
  26. Public Boolean isleaf (){
  27. Return (this. getchildren (). Size () = 0 );
  28. }
  29. Public Boolean isparentof (TREE tree ){
  30. If (tree = NULL | (basetree) Tree). Equals (this )){
  31. /* If the peer is empty */
  32. Return false;
  33. } Else if (this. isleaf ()){
  34. /* If you are a leaf, false is returned */
  35. Return false;
  36. } Else if (tree. isroot ()){
  37. /* If the other party is the root, false is returned */
  38. Return false;
  39. } Else {
  40. Basetree bt = (basetree) (tree. getparent ());
  41. If (this. Equals (BT )){
  42. /* If the parent node of the other party is itself, true is returned */
  43. Return true;
  44. } Else {
  45. /* Determine whether the parent node of the peer node is its own child and perform recursion */
  46. Return isparentof (BT );
  47. }
  48. }
  49. }
  50. Public Boolean ischildof (TREE tree ){
  51. Return (tree. isparentof (this ));
  52. }
  53. Public void addchild (TREE tree ){
  54. Children. Add (tree );
  55. }
  56. Public void rmchild (TREE tree ){
  57. Children. Remove (tree );
  58. (Basetree) Tree). setparent (null );
  59. }
  60. Public set getallleaves (){
  61. Set set_old = This. getallchildren ();
  62. Set set = new hashset ();
  63. Set. addall (set_old );
  64. Iterator itr = set_old.iterator ();
  65. While (itr. hasnext ()){
  66. Basetree bt = (basetree) itr. Next ();
  67. If (! Bt. isleaf ()){
  68. Set. Remove (BT );
  69. }
  70. }
  71. Return set;
  72. }
  73. Public set getallchildren (){
  74. Set set = new hashset ();
  75. Stack stack = new stack ();
  76. Stack. Push (this );
  77. While (! Stack. Empty ()){
  78. Basetree bt = (basetree) stack. Pop ();
  79. Set. Add (BT );
  80. Iterator itr = Bt. getchildren (). iterator ();
  81. While (itr. hasnext ()){
  82. Basetree btchild = (basetree) itr. Next ();
  83. Stack. Push (btchild );
  84. }
  85. }
  86. Set. Remove (this );
  87. Return set;
  88. }
  89. Public list getmeandlistallchildren (){
  90. List lst = new vector ();
  91. Lst. Add (this );
  92. Iterator itr = This. getchildren (). iterator ();
  93. While (itr. hasnext ()){
  94. Basetree bt = (basetree) itr. Next ();
  95. Lst. addall (Bt. getmeandlistallchildren ());
  96. }
  97. Return lst;
  98. }
  99. Abstract Public set getchildren ();
  100. Public void addobject (Object OBJ ){
  101. Objects. Add (OBJ );
  102. }
  103. Public void rmobject (Object OBJ ){
  104. Objects. Remove (OBJ );
  105. }
  106. Abstract Public set getobjects ();
  107. Public void setparent (tree parent ){
  108. This. Parent = (basetree) parent;
  109. }
  110. Public void setchildren (set children ){
  111. This. Children = children;
  112. }
  113. Public void setobjects (set objects ){
  114. This. Objects = objects;
  115. }
  116. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.