0

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

2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

even if i set the message like this: @ValidBlock(message="That's a error") ?
Hibernate Validator always gives you nasty exceptions with a useless message when it finds the error during flush time. If you want user friendliness you need to run the validator yourself ahead of time. If you find a way around this I am eager to hear about it :)
@Affe Take a look at javax.validation.ConstraintViolationException class. As you can see, the Set<ConstraintViolation<?>> constraintViolations property is the same thing returned when you manually calls the validator.validate(obj). So, it is expected that you catch this exception and use these constraintViolations as you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.