3 Replies Latest reply on Apr 27, 2007 3:57 AM by lindsayh

    Do I need to use FlushMode.MANUAL for this??

    lindsayh

      Hello,

      I have a form where a user can modify their details, including their username. So, when user saves form, I want to check that the username is unique in the database. Here's the code I use to do this (where casUser is the entity that's backing my form):

       Query usernameQuery = casDatabase
       .createQuery("select c from CasUser c where upper(username) = upper(:username)");
       usernameQuery.setParameter("username", casUser.getUsername());
       List<CasUser> usernameResults = usernameQuery.getResultList();
       if (usernameResults.size() > 0 && !usernameResults.get(0).equals(casUser))
       {
       facesMessages.addToControl("username",
       "Another user exists with this username");
       return false;
       }
       return true;
      


      Unfortunately, just before the query is executed, the entity I'm checking is automatically updated in the database, causing a UniqueConstraintViolation. This is precisely what i want to avoid by doing the check!

      I can get around this by setting the conversation's flushMode to MANUAL. But I don't really like to do this. I like to stick with the defaults for as long as I possibly can!

      Is this the only way to stop it updating the database before I've done my validation checks??

      Many thanks.

      Lindsay