title: How to disable Hibernate validation in a Spring Boot project
body: <p>I have a spring boot project that has a CrudRepository, an Entity and a Controller. I am basically trying to persist an entity based on the data passed to the Controller.</p>
  
  <p>To do this, I am using <code>spring-boot-starter-jpa</code>. My Entity is annotated with JSR-303 annotations, which are checked in the controller <em>before</em> the data gets passed to the CrudRepository for persistence.</p>
  
  <p>Controller method:</p>
  
  <pre><code>@RequestMapping(value = "users", method = { RequestMethod.POST })
  public SuccessfulResponse&lt;User&gt; addUser(@Valid @RequestBody User user, BindingResult validation) {
      if (validation.hasErrors()) {
          throw new ValidationException(validation);
      }
      User saved = this.users.save(user);
      return new SuccessfulResponse&lt;User&gt;(saved);
  }
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>@Entity /* JPA */
  public class User {
  
     @Id /* JPA */
     @Column(name="email_address", nullable=false, length=255) /* JPA */
     @UserUnique
     private String emailAddress;
  
  }
  </code></pre>
  
  <p>The cause of my issues is the <code>UserUnique</code> annotation. Its validator looks like this:</p>
  
  <pre><code>public class UserUniqueValidator implements ConstraintValidator&lt;UserUnique, String&gt; {
  
     private UserRepository users;
  
     @Autowired
     public UserUniqueValidator(UserRepository users) {
         this.users = users;
     }
  
     @Override
     public void initialize(UserUnique annotation) {}
  
     @Override
     public boolean isValid(String value, ConstraintValidatorContext context) {
         return users.findOne(value) == null;
     }
  }
  </code></pre>
  
  <p>What seems to be happening is, the validation is getting run twice. Once in the controller via the <code>@Valid</code> annotation, and once when Hibernate tries to persist the object. However, when Hibernate tries to persist the object, it throws:</p>
  
  <pre><code>javax.validation.ValidationException: HV000064: Unable to instantiate ConstraintValidator: class test.UserUniqueValidator`
  </code></pre>
  
  <p>This seems to be because its not spring-aware and cant inject the dependency into the constructor. So, what I want to do is disable Hibernate validation completely (as its redundant and already happening in the controller).</p>
  
  <p>There seems to be a property called <code>javax.persistence.validation.mode</code> which you can set to <code>none</code>. However, I cant for the life of me figure out where to set it in a code-based configuration.</p>
  
  <p>I realise there are questions like <a href="https://stackoverflow.com/questions/2712345/jsr-303-dependency-injection-and-hibernate">JSR-303 dependency injection and Hibernate</a> but these are all using xml config and manually configuring parts of the persistence layer.</p>
  
  <p>What I want to do is "post-configure" the required parts of the persistence layer that Spring Boot creates for me because if I define my own then I am no longer leveraging Spring Boot's auto configuration. Can anyone help me determine if 1) this is possible and 2) which parts do I need to configure and how?</p>
  
  <p>Thanks!</p>