2011-01-23 30 views
5

Khung: JQuery, Spring, Hibernate, Hibernate Validator (JSR-303 Bean Validation)
điều hành: Windowsjavax.validation: Nhận lỗi 'Không validator có thể được tìm thấy cho loại hình:'

Tôi cố gắng để xác định ràng buộc tùy chỉnh @IdMustExist bằng cách sử dụng Xác thực JSR 303-Bean. Mục đích của ràng buộc là kiểm tra xem giá trị id đã nhập có tồn tại trong bảng được liên kết hay không. Tôi nhận được lỗi 'javax.validation.UnexpectedTypeException: Không thể tìm thấy trình xác thực cho loại: com.mycompany.myapp.domain.package1.class1'.

Nếu tôi đặt @IdMustExist trên định nghĩa trường của Class1 (như được đưa ra trong mã ví dụ bên dưới), tôi nhận được lỗi ở trên. Nhưng nếu tôi đặt ràng buộc @IdMustExist trên một trường String, tôi không nhận được lỗi trên. Mã của tôi bị treo trong IdMustExistValidator.java. Tôi không hiểu tại sao Hibernate có thể tìm Validator cho lớp 'String' nhưng không phải cho lớp miền 'Class1'.

Định nghĩa lớp học của tôi như sau.

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 (Xin lưu ý rằng thiết kế lớp này có lỗi)

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

}

Trả lời

4

Trình xác thực ràng buộc của bạn được khai báo để xác thực giá trị Serializable. Có lẽ Class2 không phải là Serializable?

+0

Bạn đúng. Class2 của tôi không thực hiện Serializable. Cảm ơn! rất nhiều. – Jayaprakash

+1

Không phải là Class1 nên triển khai giao diện Serializable? Hoặc cả hai lớp học? – Stephane

Các vấn đề liên quan