简体   繁体   中英

Spring+Hibernate validation: throws Hibernate Exceptions and not Spring exceptions

I have a simple project (details below) which has a Spring bean and uses HV are validation provider, used for method parameter validation.

Problem: When validation fails, it throws Hibernate exception ( org.hibernate.validator.method.MethodConstraintViolationException ). But I would expect it to throw Spring exception ( org.springframework.web.bind.MethodArgumentNotValidException ), 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.

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

Question 2: If Hibernate exception is the norm, how do I map it to a friendly message (could not find this info 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: 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) ...

If you look at the inner workings of spring's MethodValidationInterceptor there is a private static class HibernateValidatorDelegate within buildValidatorFactory. Spring is making the called to configure Hibernate's HibernateValidator which is doing the actual real validation, hence why it your seeing org.hibernate.validator.method.MethodConstraintViolationException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM