javax.validation:收到错误“找不到类型的验证器:”
框架:JQuery、Spring、Hibernate、Hibernate Validator(JSR-303 Bean Validation)
平台:Windows
我正在尝试使用 JSR 303-Bean 验证定义自定义约束@IdMustExist。约束的目的是检查输入的id值是否存在于关联表中。我收到错误“javax.validation.UnexpectedTypeException:找不到类型:com.mycompany.myapp.domain.package1.class1”的验证器。
如果我将 @IdMustExist 放在 Class1 的字段定义上(如下面的示例代码所示),我会收到上述错误。但是如果我在字符串字段上放置 @IdMustExist 约束,我不会收到上述错误。我的代码在 IdMustExistValidator.java 中崩溃。我不明白为什么 Hibernate 可以找到“String”类的验证器,但不能找到域类“Class1”的验证器。
我的类定义如下。
Class2.java
@Entity
@Table
public class Class2 extends BaseEntity {
/**
* Validation: Class1 Id must exist.
*/
@ManyToOne
@JoinColumn(name="Class1Id")
@IdMustExist(entity=Class1.class)
private Class1 class1;
..
IdMustExist.java
@Required
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = IdMustExistValidator.class)
@Documented
@ReportAsSingleViolation
public @interface IdMustExist {
String message() default "{com.mycompany.myapp.domain.validation.constraints.idmustexist}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* The entity class that contains the id property.
*/
Class<?> entity();
/**
* The property of the entity we want to validate for existence. Default value is "id"
*/
String idProperty() default "id";
}
IdMustExistValidator.java(请注意,该类设计有错误)
public class IdMustExistValidator implements ConstraintValidator<IdMustExist, Serializable> {
/**
* Retrieve the entity class name and id property name
*/
public void initialize(IdMustExist idMustExist) {
if (idMustExist.entity() != null)
entity = idMustExist.entity();
idProperty = idMustExist.idProperty();
}
/**
* Retrieve the entity for the given entity class and id property.
* If the entity is available return true else false
*/
public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
logger.debug("Property Class = {}", property.getClass().getName());
logger.debug("Property = {}", property);
List resultList = commonDao.getEntityById(entity.getName(), idProperty);
return resultList != null && resultList.size() > 0;
}
private Class<?> entity;
private String idProperty;
@Autowired
private CommonDao commonDao;
private final Logger logger = LoggerFactory.getLogger(IdMustExistValidator.class);
}
Framework: JQuery, Spring, Hibernate, Hibernate Validator(JSR-303 Bean Validation)
Platform: Windows
I am trying to define a custom constraint @IdMustExist using JSR 303-Bean Validation. The purpose of the constraint is to check whether the entered id value exists in associated table. I am receiving an error 'javax.validation.UnexpectedTypeException: No validator could be found for type: com.mycompany.myapp.domain.package1.class1'.
If I place @IdMustExist on a field definition of Class1 (as given in example code below), I receive the above error. But If I place @IdMustExist constraint on a String field, I do not receive the above error. My code crashes in IdMustExistValidator.java. I dont understand why Hibernate can find Validator for 'String' class but not for domain class 'Class1'.
My class definitions are as follows.
Class2.java
@Entity
@Table
public class Class2 extends BaseEntity {
/**
* Validation: Class1 Id must exist.
*/
@ManyToOne
@JoinColumn(name="Class1Id")
@IdMustExist(entity=Class1.class)
private Class1 class1;
..
IdMustExist.java
@Required
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = IdMustExistValidator.class)
@Documented
@ReportAsSingleViolation
public @interface IdMustExist {
String message() default "{com.mycompany.myapp.domain.validation.constraints.idmustexist}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* The entity class that contains the id property.
*/
Class<?> entity();
/**
* The property of the entity we want to validate for existence. Default value is "id"
*/
String idProperty() default "id";
}
IdMustExistValidator.java (Please note that this class design has bugs)
public class IdMustExistValidator implements ConstraintValidator<IdMustExist, Serializable> {
/**
* Retrieve the entity class name and id property name
*/
public void initialize(IdMustExist idMustExist) {
if (idMustExist.entity() != null)
entity = idMustExist.entity();
idProperty = idMustExist.idProperty();
}
/**
* Retrieve the entity for the given entity class and id property.
* If the entity is available return true else false
*/
public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
logger.debug("Property Class = {}", property.getClass().getName());
logger.debug("Property = {}", property);
List resultList = commonDao.getEntityById(entity.getName(), idProperty);
return resultList != null && resultList.size() > 0;
}
private Class<?> entity;
private String idProperty;
@Autowired
private CommonDao commonDao;
private final Logger logger = LoggerFactory.getLogger(IdMustExistValidator.class);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的约束验证器被声明为验证
Serialized
值。也许
Class2
不是Serialized
?Your constraint validator is declared to validate
Serializable
values.Perhaps
Class2
is notSerializable
?