【问题标题】:JPA: Own Annotation to bundle annotations for fields with same valueJPA:Own Annotation 为具有相同值的字段捆绑注释
【发布时间】:2011-10-16 22:13:54
【问题描述】:

我想知道如何为具有相同注释的字段编写自己的注释。

例如:

老班:

public class XXXEntity {

@NotNull  
@SomeAnnotation_0  
@SomeAnnotation_1  
@Size(max = 100)  
private String email_0;

@NotNull  
@SomeAnnotation_0  
@SomeAnnotation_1  
@Size(max = 100)  
private String email_1;

...
}

想要的班级:

public class XXXEntity {

@MyOwnAnnotation  
private String email_0;

@MyOwnAnnotation  
private String email_1;

...
}

有什么提示吗?

谢谢
强尼

【问题讨论】:

  • 我不明白这与 JPA 有什么关系。你是较早的注解JPA注解还是bean验证注解,新的注解和旧的有什么关系?注意 - @Size@NOtNull 是 bean 验证注释,可用于验证实体,但它们可以在其他地方使用。

标签: java validation jpa annotations


【解决方案1】:

javax.validationconstraint composition。您可以将所有验证注释捆绑到一个自定义注释中。要从文档中复制示例:

@NotNull
@Size(min = 2, max = 14)
@CheckCase(CaseMode.UPPER)
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface ValidLicensePlate { .. }

但是,对于非验证注释,您不能这样做。

【讨论】:

    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2022-10-15
    • 2017-04-21
    • 2013-10-23
    相关资源
    最近更新 更多