Question

I've been reading a lot about the Bean Validation API that accompanies Java EE 6+ and I understand the basics of how the validation api works, but in the documentation that I have been reading, all of the examples are unit tests which does not help me understand WHERE to implement the validation operations.

I am developing a three tier architecture system. I would like to place the validation at the service layer, so I can reuse the validation code if the presentation layer differs (ie. Jax-RS, JSF, etc..). But I am confused on how to implement said operations. Here is where I am stuck:

I have beans which interact with the different entities in my model. For example this is a method within my bean for user interaction ->

public User getUser(
            @Min(value = 0, message = "Must have a positive userId") int uid)
            throws RetrievalNotFoundException {

        try {

            // I WANT TO VALIDATE UID HERE

            // find User with provided uid
            User foundUser = em.find(User.class, uid);

            // IF the user is inactive
            if (foundUser.getIsActive() == 0) {
                // cannot find the content
                throw new RetrievalNotFoundException();
            }

            // close the entity manager
            em.close();
            // return the user
            return foundUser;


    }

Here is the example from the hibernate documentation:

Car object = new Car( "Morris" );
Method method = Car.class.getMethod( "drive", int.class );
Object[] parameterValues = { 80 };
Set<ConstraintViolation<Car>> violations = executableValidator.validateParameters(
        object,
        method,
        parameterValues  
);

assertEquals( 1, violations.size() );
Class<? extends Annotation> constraintType = violations.iterator()
        .next()
        .getConstraintDescriptor()
        .getAnnotation()
        .annotationType();
assertEquals( Max.class, constraintType );

Am I really supposed to instatiate the bean again to access its method getUser()? I am confused. Another question I had was what happens if someone decides to put in an int for uid that overflows the int container? How would I validate that?

Thanks so much for your help, I really appreciate it.

Was it helpful?

Solution

A few comments to your question. First off, Bean Validation is indeed part of EE 6 and EE 7. However, EE 6 only includes Bean Validation 1.0, whereas EE 7 includes Bean Validation 1.1. The difference is that Bean Validation 1.0 does not yet include method validation and that's what you show in your examples. Hibernate Validator contains since version 4 a Hibernate Validator specific method validation API, but this is not part of the standard and slightly differs from what is specified in Bean Validation 1.1 and Hibernate Validator 5.

The second comment is regarding the code needed to execute method validation. Bean Validation only provides the mechanism to do method level validation. This is the API you are referring to in your example. In most cases you need some sort of interception technology to make use of it. Java EE 7 for example does method validation per default using CDI interceptors. It is part of the standard. See http://beanvalidation.org/1.1/spec/#integration-cdi. If you want to use EE 6 you will need to write your own interception logic using the technology of your choice.

Regarding the last question of yours. I don't think that a overflow is detectable in general. There is nothing Bean Validation can do in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top