【问题标题】:Compound Validation annotations化合物验证注释
【发布时间】:2014-01-24 22:59:34
【问题描述】:

我希望该组将一些注释合二为一,然后需要这些注释与 Hibernate-validator 和 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;

    [...]
}

我可以这样做吗?

【问题讨论】:

  • 您想将 JPA 模式标记和 JSR-303 验证组合到一个注释中...?不,没有罐头支持:(
  • @Affe 是的,我很害怕,但我希望有一些方法可以在 Hibernate 中做类似的事情。

标签: java hibernate annotations hibernate-validator


【解决方案1】:

不,这是不可能的。 Bean Validation 的组合特性只允许将几个约束注解聚合成一个新的更高级别的约束,但这并没有考虑到任何 JPA 注解。

【讨论】:

    【解决方案2】:

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

    我建议你阅读手册,它相当简单: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多