"Summary of Project issues" 5: Cascade Delete logic for tree-structured nodes

Source: Internet
Author: User

Problem Description:

A very common problem arises when you change a bug to an organization type management module, as shown in the following figure:


When the user is in the delete operation, if the tree structure of the middle node is deleted, the entire page of data disappears, such as the name of the image in the "College" node is deleted, then the entire tree structure of the data are gone, this is a very obvious bug.

Problem Analysis:

For such a obvious bug, what causes. At the beginning of the operation of the page did not know where the problem, and then open the database corresponding to the table structure to find that the tree structure corresponding to the database table is a self-related table. When I delete the middle node (non-leaf node) in the page, the child nodes under the node are not deleted, compared with the database.

Find the key to the problem, the following will find ways to solve it. This is a cascade delete scenario, when the parent node is deleted, all the child nodes under it should also be deleted. Prior to using Hibernate, we know that Hibernate has a cascade attribute in its mapping relationship with all, delete, Save-update, and none, and their respective meanings are understood. Where delete represents a cascade operation to delete the object associated with the object when the delete is executed.

The following question is whether the ORM framework used by the project is a setting that supports cascading operations. The ORM framework used in this project is Eclipselink + JPA, I think of the end, not used Ah, then go online to find relevant information, see Eclipselink cascade Operation How to set, found little data, mostly in English, And I didn't find how to set something like the Cascade property setting, so I started thinking about writing my own code for cascading deletions.

When I analyze the code, I find that the underlying deletion method is not the real delete, but the so-called "soft delete", that is, in the table structure using the Isdelete field to indicate whether the data record exists, at this time my heart is broken, this thing how the whole ah. Can the framework solve the problem of cascading soft deletion? Maybe,but I don ' t know. This strengthened my own determination to write code to achieve cascading deletions.

Problem Solving:

The solution has been finalized, so the rest is coding. To implement Cascade deletion (update), it is necessary to introduce the idea of "recursion", we must design a recursive method to traverse down from the upper node of the tree structure, until we find the leaf node of the branch, delete it, and then delete it in one layer after another, and finally the node is deleted with all its descendants. Let's look at the specific code implementation.

/** * Update the Isdelete field value of the node information, set to 1 (0: exists 1: delete) */@Override public boolean updatenodelevel (String nodeid,string Databasen
		AME) {String variable = "H.isdelete=:isdelete";
		String condition = "where h.id = '" + Nodeid + "'"; 
		map<serializable,serializable> map = new hashmap<serializable, serializable> ();
		
		Map.put ("Isdelete", 1);
	return institutiontypeeao.updatebyconditiongeneric (variable, condition, dataBaseName, map, "H"); }/** * Recursively deletes each child node * @param nodeid * @param dataBaseName */private void Recursiondelnode (String nodeid,string da
		Tabasename) {//query all child nodes of the node list<nodelevel> Nodelevellist=findchildlevelsbyid (Nodeid,databasename);
				Determines whether the node has child nodes if (nodelevellist.size () >0) {///Joaodo child nodes, recursively iterates through all child nodes for (int i=0;i<nodelevellist.size (); i++) {
			Recursive invocation of Recursiondelnode (Nodelevellist.get (i). GetId (), dataBaseName);
	}}//delete Updatenodelevel (nodeid,databasename) directly if there are no child nodes; /** * Delete node and all child node information for node */@Override Public Boolean Deletenodelevel (String nodeId, String dataBaseName) {Boolean flag=true;
		try{Recursiondelnode (nodeid,databasename);
			}catch (Exception e) {e.printstacktrace ();
		Flag=false;
	} return flag; }

        In fact, the use of the framework for our packaged services is easy and fast, but for our ability to improve is not a good thing. The most a programmer, do not ask you to write how tall on the framework and algorithm, but the basic things still need us to grasp, know it, know its why.

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.