Marcelo Claudio
Marcelo Claudio

Reputation: 3

Hibernate Class level validation

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:

ValidBlock.java

@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ValidBlockValidator.class)
public @interface ValidBlock {
    String message() default "{app.ValidBlock.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

ValidBlockValidator.java

public class ValidBlockValidator implements ConstraintValidator<ValidBlock, Block> {

   @Override
   public void initialize(ValidBlock aBlock) {}

   @Override
   public boolean isValid(Block value, ConstraintValidatorContext context) {
      return false;
    }
}

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.

Block.java

@ValidBlock
public class Block{
   ...
}

and the error:

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)

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}'} ]

Upvotes: 0

Views: 4792

Answers (1)

Perception
Perception

Reputation: 80603

You need to create a ValidationMessages.properties file and make it available at the root of your classpath, in order for the validation framework to extrapolate your custom message.

Upvotes: 1

Related Questions