title: Spring+Hibernate validation: throws Hibernate Exceptions and not Spring exceptions
body: <p>I have a simple project (details below) which has a Spring bean and uses HV are validation provider, used for method parameter validation.</p>
  
  <p>Problem: When validation fails, it throws Hibernate exception (<code>org.hibernate.validator.method.MethodConstraintViolationException</code>). But I would expect it to throw Spring exception (<code>org.springframework.web.bind.MethodArgumentNotValidException</code>), since Spring is the wrapping interface. For all I know, I do not care who is the validation implementor and need to deal with only Spring defined classes.</p>
  
  <p>Question 1: Is the above argument right and should it throw Spring exceptions?</p>
  
  <p>Question 2: If Hibernate exception is the norm, how do I map it to a friendly message (could not find this info on Google)</p>
  
  <p><strong>ProductManager.java:</strong></p>
  
  <pre><code>@Component
  @Validated
  public class ProductManager {
  
      public void createProduct(@Valid Product product) {
  
      }
  }
  </code></pre>
  
  <p><strong>Product.java:</strong></p>
  
  <pre><code>public class Product {
  
      private int id;
  
      @NotNull
      private String name;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p><strong>Tester.java:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/META-INF/applicationContext.xml"})
  public class Tester {
  
      @Autowired
      ProductManager productManager;
  
      @Test
      public void testCreateProduct() {
          Product p = new Product();
  
          try {
              productManager.createProduct(p);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p><strong>applicationContext.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.gammay.example" /&gt;
  
      &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"&gt;
          &lt;qualifier value="validator"/&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Exception printed in Tester.java:</strong></p>
  
  <blockquote>
    <p>org.hibernate.validator.method.MethodConstraintViolationException: The
    following constraint violations occurred:
    [MethodConstraintViolationImpl [method=public void
    com.gammay.example.core.ProductManager.createProduct(com.gammay.example.model.Product),
    parameterIndex=0, parameterName=arg0, kind=PARAMETER, message=may not
    be null,
    messageTemplate={javax.validation.constraints.NotNull.message},
    rootBean=com.gammay.example.core.ProductManager@12e79d0,
    rootBeanClass=class com.gammay.example.core.ProductManager,
    leafBean=com.gammay.example.model.Product@92acdc, invalidValue=null,
    propertyPath=ProductManager#createProduct(arg0).name,
    constraintDescriptor=ConstraintDescriptorImpl{annotation=javax.validation.constraints.NotNull,
    payloads=[], hasComposingConstraints=true,
    isReportAsSingleInvalidConstraint=false, elementType=FIELD,
    definedOn=DEFINED_LOCALLY, groups=[interface
    javax.validation.groups.Default],
    attributes={message={javax.validation.constraints.NotNull.message},
    payload=[Ljava.lang.Class;@7ce531,
    groups=[Ljava.lang.Class;@1ab0086}}]]     at
    org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:91)
    ...</p>
  </blockquote>