"Summary of Project issues" 4: Duplicate validation logic for modifying operations

Source: Internet
Author: User
Tags repetition

Problem Description:

In the process of making the basic system bug debugging and modification, there is a very strange problem: in the record modification operation, as shown in the figure below,


The theoretical hierarchy name and comments can be arbitrarily modified, but if I just modify the notes, do not make any changes to the name (or just click on the modified pop-up box, do not make any changes), click OK to submit, always pop up the hierarchy name repeated prompts, resulting in the modification failed.

Problem Analysis:

After a variety of tests, the problem is found in whether the name is repeated in the judgment logic, open the code to view, find the verification hierarchy name is duplicated code, as follows:

public string Validatelevelname (string levelname, String dataBaseName)
			throws Exception {
		map<serializable , serializable> map = new hashmap<serializable, serializable> ();
		list<schoollevel> classlist = new arraylist<schoollevel> ();
		String result;
		Classlist = Schoollevelbean.querylevelnodebyname (Levelname,databasename<span style= "font-family:arial, Helvetica, Sans-serif; " >);</span>
		if (classlist = = NULL | | classlist.isempty ()) {
			//No duplicate data
			result = "0";
		} else {
			/ /have duplicate data
			result = "1";
		}
		return result;
	}

The Querylevelnodebyname method of its invocation is as follows:

<span style= "FONT-SIZE:18PX; Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><pre name= "code" class= "java" >public list<schoollevel> querylevelnodebyname (String Condition,
			string dataBase) {

		string hql = "from Schoollevel h where H.levelname=:levelname and h.isdelete=0"; 
  map Map = new HashMap ();
		Map.put ("LevelName", condition);
		

		Call the underlying HQL Query method
		return schoolleveleao.querybyhql (HQL, map, dataBase);
	}

As can be seen from the above code, the method of judging whether the name is repeated is only queried by name, judging whether the returned result is empty, that is, whether the conclusion is repeated, this logic is wrong, why say so. Since it is a modification operation, then the load of this record in the database is present, and then with the existing name as a parameter to the database query, the results will certainly be able to find at least one, and the query results returned data is the page loaded out of the record, with such logic to judge whether the repetition, The result will always be "sorry, the hierarchy name already exists, please return the modification." ”。

How to do it. The first idea was that since it was a change, there was no need to repeat the judgment, but only when it was added, the repetition of the judgment. But after modifying the code, found or not, the above problem is not, and come out new problems, there has been repeated records, because there is no repetition of the check, the name can be unbridled repetition, looking back to this method is really too young too simple.

Only later began to seriously find a solution, the core problem is that in the repetition of the check, the record will be modified itself to exclude. How to exclude it. I think of two ideas, one is to modify the query statement, add "ID <> to modify the ID of the record" on the query condition. Another is to exclude the modified records from the query results, and if you exclude the submitted records, the query result set has records, which proves that the names of other records other than itself are duplicated.

Problem Solving:

Thought has, then started to modify the code logic, first tried the first to do, modify the query JPQL statement, because the JPQL statement is not familiar with, although similar to hql, but still some differences, I know the first method is simple, but did not go deep, and finally did not succeed. Therefore, the second method to implement, after repeated testing and modification, finally succeeded, its code is as follows:

public string Validatelevelname (String levelname,string levelid, String dataBaseName) throws Exception {Map<seri
		alizable, serializable> map = new hashmap<serializable, serializable> ();
		list<schoollevel> levelidlist = new arraylist<schoollevel> ();
		list<schoollevel> classlist = new arraylist<schoollevel> ();
		String result = "";
		Query according to the submitted id levelidlist = SCHOOLLEVELBEAN.QUERYLEVELIDBYHQL (Levelid, dataBaseName); Query according to the name of the submission classlist = SCHOOLLEVELBEAN.QUERYLEVELNAMEBYHQL (levelname,databasename);//classlist:0 1//query based on ID The number of records is greater than 0 is the repetition of the modified logic, or the addition of the Judgment logic if (Levelidlist.size () >0) {//Modified judgment logic//the same ID, if the name of the submission and the database queried the same name, then do not repeat if (levelidl
			Ist.get (0). Getlevelname (). Equals (LevelName)) {result= "0"; }else{//If the name is changed, the record itself in the result of the name query is excluded for (int i=0;i<classlist.size (); i++) {if (Classlist.get (i). GetId () ==le
					Velid) {classlist.remove (i); }}//Determine if the number of record bars after the removal itself is 0 if (classlist.size () ==0{//If 0, the result = "0" is not repeated;
				} else {//otherwise repeat result = "1";
				
			}}}else{//Add the Judgment logic if (Classlist.size () >0) {result= "1";
			}else{result= "0";
	}} return result; }

Because the name of the added and modified names are used in this function, the logic of the two judgments is put together, and some are cumbersome. Although I realized the functional effect, but I understand that this implementation can also be optimized, and this is not the optimal implementation of the scheme, I hope everyone has a good solution can give me a message, greatly appreciated.

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.