Survey Management System-(7) Design survey options handling &answer entity Analysis Design & Development experience Skills & Database Usage

Source: Internet
Author: User
Tags uuid

1. Design investigation Option processing- time to use Hibernate to assign values to objects

  1) Add the order-by attribute in the <set> element of the hibernate mapping file to specify how the collection elements are sorted.

2) because the option of the problem is saved in the database as a string (the option is separated by "\ r \ n"), the string is split before the problem option is displayed on the page. When should the execution time of the string split be better? (Video 16: Survey Management System _ Design Investigation option processing)

Tips: Hibernate queries the data from the DB and maps it to an object, which is the setter method that invokes the object member variable when assigning a value to the object. Therefore, it is possible to use Hibernate to assign values to objects, rewrite the object setter method, add the corresponding business logic, and complete the splitting of the string.

the basic design of the question entity is as follows, which adds the corresponding getter and setter methods to each field that needs to be split, and overrides the corresponding setter method, which overrides the SetOptions method as an example.

1  PackageCom.atguigu.surveypark.model;2 ImportCom.atguigu.surveypark.util.StringUtil;3 ImportCom.atguigu.surveypark.util.ValidateUtil;4 /**5 * Problem class6  */7  Public classQuestion {8     Private Static Final String RN = "\ r \ n" ;9     PrivateInteger ID;//primary Key IDTen     Private intQuestiontype;//Types 0-8 One     PrivateString title;//problem-solving work A     PrivateStringOptions;//Options -     Private string[] Optionarr ; -     Private BooleanOther;//Other Items the     Private intOtherstyle;//Other Items style: 0-No 1-text box 2-drop-down list -     //Other Item Drop -down options -     PrivateString otherselectoptions; -     Privatestring[] Otherselectoptionarr; +     //Matrix row Header set -     PrivateString matrixrowtitles; +     Privatestring[] Matrixrowtitlearr; A     //Matrix column Header set at     PrivateString matrixcoltitles; -     Privatestring[] Matrixcoltitlearr; -     //Matrix is a drop-down option set -     PrivateString matrixselectoptions; -     Privatestring[] Matrixselectoptionarr; -     PrivatePage page;//establish a many-to-one correlation between question and page in     /** - * Override SetOptions method to complete string splitting to      */ +      Public void setoptions(String options) { -          This. options =options; the          This.Optionarr = Stringutil.str2arr (Options, RN); *     } $     //other getter and setter methods are omitted.Panax Notoginseng}

The basic implementation of the Str2arr () method in the Stringutil tool class is as follows:

 1  //  2  public  static   string[] str2arr  (String str, String Tag { 3  if   (Validateutil.isvalid (str)) { 4  return   Str.split (tag);  5   6  return  null   ; 7 } 

2. Deletion and editing of pages & issues

  1) analysis and design of the entity answer of the answer

  Ⅰ, answers and questions are a one-to-many relationship, and in this system, users consider information such as the proportion of people who answer a question, rather than the specific answer of a person. In addition, the answer entity holds the index string for the option ("," delimited) and other items, and it can also include the time to answer the question. It is also important to indicate that a certain number of answers are answered by an identity, so introduce a UUID string to represent the answer to a batch (i.e. the UUID of the answer of the same person is the same).

Ⅱ, the relationship between entities (single/bidirectional) can be established or not established, to be considered according to the needs. In this decency, there is no association between question and answer entities. Because in the investigation does not care about one person's answer, also does not go to retrieve an entity, mainly uses the aggregate function count () to statistic the answer the proportion, thus does not have the answer entity navigation to the question, Therefore, there is no need to establish a answer-to-question relationship, nor to query the results of a question, but to look at the results, so there is no need to establish an association between question to answer. When you do not establish an association relationship, the maintenance of the relationship requires only a normal field to [Q's id], without the need to use the Q object. (On-line examination system and so on according to the situation to consider the design, you can save the correct answer in Q, after the answer to compare and so on. )

The basic design of Ⅲ and answer entities is as follows:

1  PackageCom.atguigu.surveypark.model;2 Importjava.util.Date;3 /**4 * Answer5  */6  Public classAnswer {7     PrivateInteger ID;8     PrivateString Answerids;//the index of the option.9     PrivateString Otheranswer; //Other Items Ten     PrivateString uuid;//batches. One     PrivateDate answertime; A  -     PrivateInteger QuestionID;//Associated Fields -     //Association field: page ID (redundancy). Note: This field is not given in the tutorial to demonstrate Hibernate's database write operation issues the     //private Integer pageId; -     PrivateInteger Surveyid;//Association fields: survey ID (redundancy)-special fields added to increase efficiency -     //The getter and setter are omitted -}

2) Some problems in hibernate use

Ⅰ, in the system when deleting questions, pages, surveys, the corresponding answer should also be deleted. Hibernate provides a cascade delete operation, but its performance is poor, it is to delete all the child nodes of the object is all found out and then one by one delete, not bulk delete. In addition, Hibernate provides cascading operations, but rarely uses cascading in real project development, which can lead to complex problems such as our data is not controllable.

Ⅱ, Hibernate does not allow more than two links in the database write operation, need to use the method of sub-query instead. Action when deleting a survey as follows:

1 /**2 * Delete Survey while deleting page, question, answer3  */4  Public void deletesurvey(Integer sid) {5     //Delete Answers6String hql = "Delete from Answer a where A.surveyid =?" ;7 answerdao.batchentitybyhql (HQL,SID);8     // Hibernate does not allow links of more than two levels in the write Operation .9     //hql = "Delete from Question Q where q.page.survey.id =?";Tenhql = "Delete from Question Q whereq.page.id in (select P.id from page p where p.survey.id =?)" ; One questiondao.batchentitybyhql (HQL, sid); A     //Delete Page -hql = "Delete from page p where p.survey.id =?" ; - pagedao.batchentitybyhql (HQL, sid); the     //Delete Survey -hql = "Delete from Survey s where s.id =?" ; - surveydao.batchentitybyhql (HQL, sid); -}

3. Toggle status-Open and close survey

  1) In order to indicate whether the current survey is open or closed the Closed field for the survey entity was added with a Boolean type (base type, non-wrapper type) and configured in the mapping file. When you rerun the program, because the default value specified for the Closed field in the database is null,hibernate cannot assign a null value to the base data type, it causes the program to start an exception, so you need to manually modify the database closed the value of the field is true or false, To enable the program to start normally.

2) The Boolean type used in MySQL is the bit type bit (1), which is a bit bit (0 or 1). When it is displayed on the command line, a value of 1 is true, then a "smiley" symbol is displayed, and if 0 is false, it is not displayed.

3) The reverse operation is not supported in Hibernate HQL, as shown in the following example:

1 s.closed =!s.closed where s.id =? "  ; //This notation is wrong.  2

  The following is the correct wording, that is, to reverse the original value and then as a parameter in the form of

1  This . Getsurvey (SID); 2 s.closed =? where s.id =? "  ; 3 surveydao.batchentitybyhql (HQL,!s.isclosed (), sid);

  4) The default value for the Boolean type in Java is false.

  5) The Update property of the <property> element in the hibernate mapping file restricts only the object-oriented update operation that is the Update/saveorupdate method of the session , you cannot limit the HQL update.

4. Experience Summary

  1) After performing a database write operation in the background, you are redirected to a page or action instead of forwarding, which causes the form to repeat the problem when the user makes a refresh or a fallback operation.

2) when the editing/modification is complete, in the background of the save update operation, there are generally two kinds of processing methods:

Ⅰ, the model is updated directly to the database, if there is a foreign key association is reset before saving the update;

Ⅱ, first finds the original object from the database, then sets the value of the property in the model to the original object, and then saves the original object.

(To choose the appropriate way depending on how many fields are modified.) The first way should be to reduce the number of accesses to the database, because the general foreign key relationship object can be directly new to the object containing only the ID (primary key), set to model, and do not need to be isolated from the database. )

Survey Management System-(7) Design survey options handling &answer entity Analysis Design & Development experience Skills & Database Usage

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.