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
- /**
- * The basic operations of the tree are implemented, including ascending, descending, subnode addition/removal, recursive search, and object Association.
- */
- 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 code
- 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 )){
- /* If the peer is empty */
- Return false;
- } Else if (this. isleaf ()){
- /* If you are a leaf, false is returned */
- Return false;
- } Else if (tree. isroot ()){
- /* If the other party is the root, false is returned */
- Return false;
- } Else {
- Basetree bt = (basetree) (tree. getparent ());
- If (this. Equals (BT )){
- /* If the parent node of the other party is itself, true is returned */
- Return true;
- } Else {
- /* Determine whether the parent node of the peer node is its own child and perform recursion */
- 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;
- }
- }
|