title: Running @HandleBeforeCreate after entity validation in Spring Boot Data REST
body: <p>I'm using Spring Boot Data REST to persist my <code>User</code> entities</p>
  
  <pre><code>@Entity
  public class User {
  
      @Id
      @GeneratedValue
      private long id;
  
      @NotEmpty
      private String firstName;
  
      @NotEmpty
      private String lastName;
  
      @NotEmpty
      private String email;
  
      @Size(min = 5, max = 20)
      private String password;
  
      // getters and setters
  }
  </code></pre>
  
  <p>using the repository:</p>
  
  <pre><code>public interface UserRepository extends CrudRepository&lt;User, Long&gt; {}
  </code></pre>
  
  <p>What I want to do is validate the <code>POST</code>ed users first:</p>
  
  <pre><code>@Configuration
  public class CustomRestConfiguration extends SpringBootRepositoryRestMvcConfiguration {
  
      @Autowired
      private Validator validator;
  
      @Override
      protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
          validatingListener.addValidator("beforeCreate", validator);
      }
  
  }
  </code></pre>
  
  <p>and only later hash the user's password before storing it in the DB:</p>
  
  <pre><code>@Component
  @RepositoryEventHandler(User.class)
  public class UserRepositoryEventHandler {
  
      private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  
      @HandleBeforeCreate
      public void handleUserCreate(User user) {
           user.setPassword(passwordEncoder.encode(user.getPassword()));
      }
  }
  </code></pre>
  
  <p>As it turns out though, validation is performed after the password hashing and as a result it fails due to the <em>hashed</em> password being too long. </p>
  
  <p>Is there any way to instruct Spring to perform validation first and only then hash the password? I know I could write a controller myself and specify everything in a fine-grained manner, but I'd rather leave it as my last resort.</p>