@Embeddable的Hibernate验证器

时间:2015-12-28 18:36:25

标签: hibernate validation bean-validation hibernate-validator

我有一个嵌入了属性的实体,并使用Hibernate验证器来验证实体。但是不对嵌入属性执行验证。

下面的代码段。

@Entity
@Table(name = "FV_TBL_SOI_IGLS")
public class IGLSDataBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -7274784548705371043L;

public IGLSDataBean() {

}

/*
 * @Column(name="FUND_CODE") private String fundCode;
 */
@Size(min = 0, max = 120)
@Column(name = "FUND_NAME")
private String fundName;
@Size(min = 0, max = 255)
@Column(name = "SECURITY_NAME")

     @Valid
@EmbeddedId
private SOIPK id;

@Embeddable
public class SOIPK implements Serializable {
@NotNull
@Column(name = "SEC_ID")
private String secid;

Set<ConstraintViolation<IGLSDataBean>> constraintViolations = validator
            .validate(iglsDataBean);

2 个答案:

答案 0 :(得分:2)

我只是遇到了同样的问题,添加注释@Valid还不够,您还需要添加诸如@NotNull之类的检查内容

    @Entity
@Table(name = "FV_TBL_SOI_IGLS")
public class IGLSDataBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -7274784548705371043L;

public IGLSDataBean() {

}

/*
 * @Column(name="FUND_CODE") private String fundCode;
 */
@Size(min = 0, max = 120)
@Column(name = "FUND_NAME")
private String fundName;
@Size(min = 0, max = 255)
@Column(name = "SECURITY_NAME")

@Valid
@EmbeddedId
@NotNull
private SOIPK id;

@Embeddable
public class SOIPK implements Serializable {
@NotNull
@Column(name = "SEC_ID")
private String secid;

Set<ConstraintViolation<IGLSDataBean>> constraintViolations = validator
            .validate(iglsDataBean);

答案 1 :(得分:1)

您忘记使用@Valid注释地址字段。

附注:为什么要通过他的地址识别员工?这没什么意义。