質問

枠組み:JQuery、春、Hibernate、Hibernate Validator(JSR-303豆の検証)
プラットフォームWindows

って定義するカスタムの制約@IdMustExist用JSR303-大豆バリデーションを実施します。目的の制約を確認するに入力したidの値が存在す関連す。を得ていますがエラー'javax.バリデーションを実施します。UnexpectedTypeException:ないバリデータが見つかのタイプ:com.mycompany.myapp.ます。package1.クラス1'.

また@IdMustExist、分野の定義にクラス1(たとえば以下のコード)を受け、上記のエラーになります。だが私は@IdMustExist制約を文字列としを受けていない上記のエラーになります。私のコードクラッシュにIdMustExistValidator.java.あまりないと思いますかHibernateでバリデータのための'文字列'クラスがドメインクラスクラス1'.

私のクラス定義は以下のとおり。

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

}

役に立ちましたか?

解決

おの制約validatorが宣言された検証 Serializable 値です。か Class2 ではない Serializable?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top