2 Replies Latest reply on Dec 6, 2007 7:33 PM by gsegura

    help with this entity been updated implicitly

    gsegura

      Hello everybody,
      I have this use case: an item is selected from a list, the edit page for that item is shown, if the save action is invoked some validation is done inside that same method, only if everything is ok I call persist, otherwise the same edit page is redisplayed.

      Everything goes fine except one thing: even if validation fails (so I don't call persist) the entity is been updated, why? how? where? I reproduce some code:

      @Scope(ScopeType.CONVERSATION)
      @Name("courseEditor")
      @Restrict("#{identity.loggedIn and s:hasRole('Administrator')}")
      public class CourseEditorAction {
      
       @Begin(nested=true)
       public void select(Course selectedCourse) {
       course = entityManager.merge(selectedCourse);
       }
      
       public void save() {
       if(course.getStartDate()!=null && course.getFinishDate()!=null &&
       course.getStartDate().compareTo(course.getFinishDate())>=0) {
       facesMessages.addToControl("finishDate","must be posterior to start date") ;
       ok = false ;
       }
       else {
       sede = entityManager.merge(sede) ;
       if(course.getId()==null)
       //my way to update parent collection of courses (only if course is new)
       sede.getCourses().add(course) ;
       entityManager.persist(course);
       facesMessages.add("Course saved successfully");
       //I only finish conversation in case the edition use case finish
       Conversation.instance().end() ;
       ok = true;
       }
       }
      
       @End
       public void cancel() {}
      
       public boolean isOk() {
       return ok ;
       }
      }


      <page view-id="/admin/courseEditor.xhtml">
       <navigation from-action="#{courseEditor.save}">
       <rule if="#{courseEditor.ok}">
       <redirect view-id="/admin/sedeEditor.xhtml"/>
       </rule>
       <rule if="#{not courseEditor.ok}">
       <redirect view-id="/admin/courseEditor.xhtml"/>
       </rule>
       </navigation>
       <navigation from-action="#{courseEditor.cancel}">
       <redirect view-id="/admin/sedeEditor.xhtml"/>
       </navigation>
      
       </page>


      if finish date value is before start date, the page is redisplayed, the message describing the incorrect input is shown, but at that moment the course already has been updated :S

      I though maybe select method was been called every time the page is redisplayed and since there is a merge that was causing the update, but no: select method is called (makes sense) only first time.
      so the updating is invoked somewhere else...
      any help is much appreciated