title: Hibernate Class level validation
body: <p>Well, I read several issues (here and other sites) about class level validation with hibernate, I created the annotation and validation class, but when validation returns false, I get an exception (when validation returns true its ok) that the class is not valid (obviously because validation returned false), my doubt is: it was not to return a validation message as well? why is returning an exception, the code:</p>
  
  <p>ValidBlock.java</p>
  
  <pre><code>@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = ValidBlockValidator.class)
  public @interface ValidBlock {
      String message() default "{app.ValidBlock.message}";
      Class&lt;?&gt;[] groups() default {};
      Class&lt;? extends Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <p>ValidBlockValidator.java</p>
  
  <pre><code>public class ValidBlockValidator implements ConstraintValidator&lt;ValidBlock, Block&gt; {
  
     @Override
     public void initialize(ValidBlock aBlock) {}
  
     @Override
     public boolean isValid(Block value, ConstraintValidatorContext context) {
        return false;
      }
  }
  </code></pre>
  
  <p>I made the test and i'm pretty sure i recieved the Block object, as when validation return true is fine i just return false to test.</p>
  
  <p>Block.java</p>
  
  <pre><code>@ValidBlock
  public class Block{
     ...
  }
  </code></pre>
  
  <p>and the error:</p>
  
  <blockquote>
    <p>Grave: javax.validation.ConstraintViolationException: Validation
    failed for classes [com.labsys.model.Block] during persist time for
    groups [javax.validation.groups.Default, ] List of constraint
    violations:[
      ConstraintViolationImpl{interpolatedMessage='{app.ValidBlock.message}',
    propertyPath=, rootBeanClass=class com.labsys.model.Block,
    messageTemplate='{app.ValidBlock.message}'} ]     at
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
      at
    org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
      at
    org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:185)
      at
    org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
      at
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
      at
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
      at
    org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
      at
    org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
      at
    org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:981)</p>
    
    <p>Grave: JSF1073: javax.faces.event.AbortProcessingException obtido
    durante o processamento de INVOKE_APPLICATION 5:
    UIComponent-ClientId=blockForm:j_idt14,
    Message=javax.validation.ConstraintViolationException: Validation
    failed for classes [com.labsys.model.Block] during persist time for
    groups [javax.validation.groups.Default, ] List of constraint
    violations:[
      ConstraintViolationImpl{interpolatedMessage='{app.ValidBlock.message}',
    propertyPath=, rootBeanClass=class com.labsys.model.Block,
    messageTemplate='{app.ValidBlock.message}'} ] Grave:
    javax.validation.ConstraintViolationException: Validation failed for
    classes [com.labsys.model.Block] during persist time for groups
    [javax.validation.groups.Default, ] List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='{app.ValidBlock.message}',
    propertyPath=, rootBeanClass=class com.labsys.model.Block,
    messageTemplate='{app.ValidBlock.message}'} ]</p>
  </blockquote>