Spring + Hibernate Check: Throws exceptions for hibernation, not Spring exceptions

I have a simple project (see below for details) that has a Spring bean and uses HV, a validation provider used to validate method parameters.

Problem. When validation fails, it throws a Hibernate ( org.hibernate.validator.method.MethodConstraintViolationException) exception . But I would expect it to throw a Spring ( org.springframework.web.bind.MethodArgumentNotValidException) exception , since Spring is a wrapper interface. As far as I know, I don't care who the validation developer is, and he only needs to deal with Spring specific classes.

Question 1: Is the above argument correct and should it throw Spring exceptions?

Question 2: If the Hibernate exception is normal, how can I match it with a friendly post (could not find this information on Google)

ProductManager.java:

@Component
@Validated
public class ProductManager {

    public void createProduct(@Valid Product product) {

    }
}

Product.java:

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;
    }

}

Tester.java:

@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();
        }
    }

}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">


    <context:annotation-config />
    <context:component-scan base-package="com.gammay.example" />

    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <qualifier value="validator"/>
    </bean>

    <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>

</beans>

Exception printed in Tester.java:

org.hibernate.validator.method.MethodConstraintViolationException: : [MethodConstraintViolationImpl [ = public void com.gammay.example.core.ProductManager.createProduct(com.gammay.example.model.Product), parameterIndex = 0, parameterName = arg0, kind = PARAMETER, message = can not , messageTemplate = {javax.validation.constraints.NotNull.message}, rootBean=com.gammay.example.core.ProductManager@12e79d0, rootBeanclass= com.gammay.example.core.ProductManager, leafBean=com.gammay.example.model.Product@92acdc, invalidValue = null, PropertyPath = # createProduct (arg0).name, constraintDescriptor = ConstraintDescriptorImpl { = javax.validation.constraints.NotNull, payloads = [], hasComposingConstraints = true, isReportAsSingleInvalidConstraint = false, elementType = FIELD, definedOn = DEFINED_LOCALLY, groups = [interface javax.validation.groups.Default], = { = {javax.validation.constraints.NotNull.message}, = [Ljava.lang.Class; @7ce531, groups = [Ljava.lang.Class; @1ab0086}}]] org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:91)...

+4
1

spring MethodValidationInterceptor, buildValidatorFactory private static class HibernateValidatorDelegate. spring Hibernate HibernateValidator, , org.hibernate.validator.method.MethodConstraintViolationException

+1

Source: https://habr.com/ru/post/1543572/


All Articles