复合验证注释

时间:2014-01-24 22:59:35

标签: java hibernate annotations hibernate-validator

我希望该组中有一些注释,然后需要注释与Hibernate验证器和Hibernate的generate-ddl一起工作

我有一堆实体,大部分都有类似的字段。例如:

@Entity 
public class Usuario implements Serializable {

    @Column(name = "ID", nullable = false)
    @Id
    private Long id;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "CODIGO", nullable = false)
    @NotNull
    private Integer code;

    @Column(name = "NOMBRE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String name;

    @Column(name = "CLAVE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String password;

    @Column(name = "ES_ADMINISTRADOR")
    @NotNull
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private Boolean isAdmin;

    [...]
}

我希望有类似这样的东西

@Entity 
public class Usuario implements Serializable {

    @Column(name = "ID", nullable = false)
    @Id
    private Long id;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @CodeField
    private Integer code;

    @Column(name = "NOMBRE")
    @NameField
    private String name;

    @Column(name = "CLAVE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String password;

    @Column(name = "ES_ADMINISTRADOR")
    @BooleanField
    private Boolean isAdmin;

    [...]
}

我能这样做吗?

2 个答案:

答案 0 :(得分:0)

不,那是不可能的。 Bean Validation的组合功能仅允许将多个约束注释聚合到新的更高级别约束中,但这不会考虑任何JPA注释。

答案 1 :(得分:0)

我不确定你想要实现什么,但就Hibernate Validator而言,可以通过实现 ConstraintValidator 接口来创建自己的约束。

我建议你阅读手册,这很容易: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html