title: One Transaction for Hibernate Validation and Spring controller
body: <p>I am trying to implement the registration controller for a Rest API. I have read about where to place @Transactional quite a bit. (Not at DAO level but at the services maybe orchestrated). In my use case I want not only services but also a hibernate validation to use the same transaction. </p>
  
  <p>This is the code of the controller:</p>
  
  <pre><code>@Autowired
  private UserService userService;
  
  @RequestMapping(method = RequestMethod.GET)
  @ResponseBody
  @Transactional
  public DefaultResponse register(@Valid RegisterIO registerIO, BindingResult errors) {
      DefaultResponse result = new DefaultResponse();
  
      if (errors.hasErrors()) {
          result.addErrors(errors);
      } else {
          userService.register(registerIO);
      }
  
      return result;
  }
  </code></pre>
  
  <p>I have written an custom contraint annotation, which validates an attribute of the parameter registerIO. Both, this validator and userService.register(registerIO); access the database (check if the email address is already in use).</p>
  
  <p>Therefore I want both methods use the same Hibernate session and transaction.</p>
  
  <p>This approach results in the following exception:</p>
  
  <pre><code>org.hibernate.HibernateException: No Session found for current thread
  org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
  org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)
  </code></pre>
  
  <p>The problem is the @Transactional annotation. When I place this annotation at the methods witch call the database everything works find but two transactions are startet. I suspect that when i place it at the register Method the hibernate validation is performed before @Transactional starts the transaction for this method. </p>
  
  <p>I developed the following functional workaround but I am not happy with it. This codes does not use the @Valid annotation but calls the validator by itself:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
  @ResponseBody
  @Transactional
  public DefaultResponse register( RegisterIO registerIO, BindingResult errors) {
      DefaultResponse result = new DefaultResponse();
  
      ValidatorFactory vf =  Validation.buildDefaultValidatorFactory();
      Validator validator = vf.getValidator();
      Set&lt;ConstraintViolation&lt;RegisterIO&gt;&gt; valResult = validator.validate(registerIO);
  </code></pre>
  
  <p>I try to summarise my question:
  Using Spring MVC and Hibernate-Validation together with @Valid and @Transactional, how is it possible to encapsulate the whole request into one transaction?</p>
  
  <p>Thank you :)</p>