title: Bean validation with EJB
body: <p>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.</p>
  
  <p>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:</p>
  
  <p>I have beans which interact with the different entities in my model. For example this is a method within my bean for user interaction -></p>
  
  <pre><code>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;
  
  
      }
  </code></pre>
  
  <p>Here is the example from the hibernate documentation:</p>
  
  <pre><code>Car object = new Car( "Morris" );
  Method method = Car.class.getMethod( "drive", int.class );
  Object[] parameterValues = { 80 };
  Set&lt;ConstraintViolation&lt;Car&gt;&gt; violations = executableValidator.validateParameters(
          object,
          method,
          parameterValues  
  );
  
  assertEquals( 1, violations.size() );
  Class&lt;? extends Annotation&gt; constraintType = violations.iterator()
          .next()
          .getConstraintDescriptor()
          .getAnnotation()
          .annotationType();
  assertEquals( Max.class, constraintType );
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks so much for your help,
  I really appreciate it.</p>