2 Replies Latest reply on Dec 7, 2006 3:56 AM by Ahmad Atwi

    Validation on update

    Ahmad Atwi Newbie

      Dear all:
      I am trying to validate the fields upon update.
      in the update method i created a query statement that checks if the a value is unique.
      here is the update function

      @IfInvalid(outcome = Outcome.REDISPLAY)
       public String update() {
       String compName = instance.getName();
       String compTel = instance.getTel();
       String compFax = instance.getFax();
       int compId = instance.getId();
      
       entityManager.refresh(instance);
      
       if (compName.equals(instance.getName())) {
       instance.setTel(compTel);
       instance.setFax(compFax);
       entityManager.flush();
       refreshFinder();
       return null;
       }
       else {
       Query query1 = entityManager.createQuery("select comp from Companies comp where comp.name=:compName");
       query1.setParameter("compName", compName);
      
       if (query1.getResultList() != null) {
       FacesContext.getCurrentInstance().addMessage(
       null,
       new FacesMessage(messages.get("Companies_name") + " "
       + messages.get("AlreadyExists")));
       return null;
       }
       }
       instance=(Companies)entityManager.find(Companies.class, compId);
       instance.setName(compName);
       instance.setTel(compTel);
       instance.setFax(compFax);
       entityManager.flush();
       refreshFinder();
       return null;
       }


      If the user tries to enter a company name that already exists in the database then an error is reported. (works fine)
      but if the user tries to enter a company name that doesnot exist in the database then an error is also reported. the problem is that in this case the query statement returns a list but it is not supposed to do so.

      any one knows how to solve that.

      Best regards
      ama55