Hibernate类级别验证

时间:2012-06-20 23:22:10

标签: java hibernate jsf

好吧,我读了几个关于使用hibernate进行类级别验证的问题(这里和其他网站),我创建了注释和验证类,但是当验证返回false时,我得到一个异常(当验证返回true时它确定) class无效(显然因为验证返回false),我的疑问是:它不是也不返回验证消息?为什么要返回一个异常,代码:

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;
    }
}

我做了测试,我很确定我收到了Block对象,因为当验证返回true时很好我只返回false进行测试。

Block.java

@ValidBlock
public class Block{
   ...
}

和错误:

  

Grave:javax.validation.ConstraintViolationException:验证   在持续时间内为类[com.labsys.model.Block]失败   groups [javax.validation.groups.Default,]约束列表   违规行为:     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)     在   org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)     在   org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:185)     在   org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)     在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)     在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)     在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)     在   org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)     在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)     在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)     在   org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:981)

     

Grave:JSF1073:javax.faces.event.AbortProcessingException obtido   durante o processamento de INVOKE_APPLICATION 5:   UIComponent-客户端Id = blockForm:j_idt14,   Message = javax.validation.ConstraintViolationException:验证   在持续时间内为类[com.labsys.model.Block]失败   groups [javax.validation.groups.Default,]约束列表   违规行为:     ConstraintViolationImpl {interpolatedMessage = '{} app.ValidBlock.message',   propertyPath =,rootBeanClass = class com.labsys.model.Block,   messageTemplate ='{app.ValidBlock.message}'}]严重:   javax.validation.ConstraintViolationException:验证失败   在组的持续时间内,类[com.labsys.model.Block]   [javax.validation.groups.Default,]违反约束的列表:[     ConstraintViolationImpl {interpolatedMessage = '{} app.ValidBlock.message',   propertyPath =,rootBeanClass = class com.labsys.model.Block,   messageTemplate ='{app.ValidBlock.message}'}]

1 个答案:

答案 0 :(得分:1)

您需要创建一个ValidationMessages.properties文件并使其在类路径的根目录下可用,以便验证框架推断您的自定义消息。