2 Replies Latest reply on Jun 24, 2009 6:31 PM by Arno Mittelbach

    NoResultException causes transaction to rollback

    Arno Mittelbach Newbie

      Hi,


      I am trying to validate an entity on persist time. For this I extended the Abstract Home class and overrode the persist method. For validating the entity I have to run a query against the database, which if successful should result in a NoResultException. If that is the case I want to continue with the persist. Here is the code I am using. The UserTreeUtil component runs the query and returns null if it catches the NoResultException



      @Name("rs.RoleHome")
      @AutoCreate
      public class RoleHome extends EntityHome<Role> {
      
           @In("rs.UserTreeUtil") private UserTreeUtil userTreeUtil;
           
           /**
            * Make sure that the unique constraint on name is validated
            */
           @Override
           @Transactional
           public String persist(){
                if(null != getInstance()){
                     Role r = userTreeUtil.getRoleByName(getInstance().getName());
                     if(null != r)
                          throw new UniqueRoleNameValidationException(r.getName());
                }
      
                return super.persist();
           }
      }
      



      The problem is, that whenever a NoResultException is thrown the transaction is marked to be rolled back and I get a javax.persistence.TransactionRequiredException: no transaction is in progress when performing the actual persist operation. Is there any way of handling the NoResultException without causing the current transaction to be rolled back?


      Thanks
      Arno