title: Why did Spring Data JPA save method didn't validate straight away?
body: <p>I'm using Spring Boot (1.3.3.RELEASE) with Spring Data and Bean Validation on a project and I found an unexpected (at least to me) behaviour. I have an User entity and its UserRepository (an interface that extends JpaRepository). When I make a call like this:</p>
  
  <pre><code>userRepository.save(user);
  logger.info("User saved with id", user.getId());
  </code></pre>
  
  <p>On a new and INvalid user, logger is called and shows that the user received an id. Even though nothing gets persisted and ConstraintViolationException is thrown at the end, after logging.</p>
  
  <p>But if I change to:</p>
  
  <pre><code>userRepository.saveAndFlush(user);
  logger.info("User saved with id", user.getId());
  </code></pre>
  
  <p>ConstraintViolationException is thrown right away and the logger line never gets reached.</p>
  
  <p>This second behaviour, where the exception is thrown before the logger, is what I believe it would have to happen when I call userRepository.save() method.</p>
  
  <p>What is wrong? Something about my project? Or my understanding on how bean validation is supposed to work?</p>