10 Replies Latest reply on Aug 15, 2007 5:26 AM by christian.bauer

    validation and persisting

    mvlach

      Hi,

      I have problem with validation on the server side. Let's imagine I change data on the web form and submiting. On the call I decide that combination is wrong and add message to the FacesMessage and return null. But problem is, that some changes are saved to the database.

      Have I to throw some exception or I have to return null only ?

      Thanks Mila

        • 1. Re: validation and persisting
          pmuir

          Throw an exception which causes a roll back, or, IMO a better option is to use the manual flush mode pattern./

          • 2. Re: validation and persisting
            mvlach

            Where can I read some about manual flushing ?

            Only set
            @Begin(flushMode....)

            and

            em.flush()

            ?
            Or something special ?

            Thanks Mila

            • 3. Re: validation and persisting
              pmuir

              AS you write.

              • 4. Re: validation and persisting
                mvlach

                But how to do with this situation:

                there is no @Begin annotated method. Page is displayed and when clicking the button actionlistener is called.


                public String save() {
                }

                How to do in this situation ?
                Thanks Mila

                • 5. Re: validation and persisting
                  christian.bauer

                  Easy: Don't modify your data. If you do not have a conversation, then the entity instances have to be loaded by you from the database and it is you who are modifying these instances. Do your validation before you modify the instances.

                  • 6. Re: validation and persisting
                    christian.bauer

                    If you want more specific help, as always, post your code.

                    • 7. Re: validation and persisting
                      mvlach

                      There is code....

                      @Name("requestAction")
                      @Stateful
                      @Restrict("#{identity.loggedIn}")
                      public class RequestAction implements RequestActionLocal, Serializable {
                      
                      ....
                      


                      ....
                       @ValidatedMethod(formName = "editForm", validators = { @CustomValidator(validatorClass = RequestSaveValidator.class, mapping = { "category", "requestType",
                       "request" }) })
                       @End(beforeRedirect = true)
                       public String update() {
                       log.info("adding the request #0", request);
                       em.refresh(category);
                       em.refresh(requestType);
                      
                       if (!requestType.getRequests().contains(request)) {
                       requestType.getRequests().add(request);
                       }
                       request.setCategory(category);
                       request.setAuthor(loggedUser);
                       request.setCreated(new Date());
                       request.setRequestType(requestType);
                      
                       em.persist(request);
                       em.flush();
                       return DefaultActions.EDITED;
                       }
                      
                       @Begin(join = true, flushMode=FlushModeType.MANUAL)
                       public String loadDetail() {
                       edit();
                       return DefaultActions.LOADED;
                       }
                      


                      • 8. Re: validation and persisting
                        christian.bauer

                        And? All I can see is that you probably do not know how refresh() is used properly (in 5 years of Hibernate I never needed it).

                        • 9. Re: validation and persisting
                          mvlach

                          I remove the resfresh, but problem is,when validation failed some attributes are set and persisted to the entity Requst.

                          • 10. Re: validation and persisting
                            christian.bauer

                            Then that's the problem of how @ValidatedMethod works, which I guess is something you wrote.