注释聚合

Tit*_*ito 6 java annotations duplicates aggregation

我有多个类,我总是在定义表的主键的字段上使用相同的注释,例如:

@Id 
@Type(type = "uuid-binary")
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2", 
parameters = { @Parameter(
        name = "uuid_gen_strategy_class", 
        value = "org.hibernate.id.UUIDGenerationStrategy") 
})
@Column(name="PROFILE_ID", unique = true)
@NotNull(message = "we have one message" , payload =Severity.Info.class)
private UUID profileId;
Run Code Online (Sandbox Code Playgroud)

现在我正在寻找一种方法,在我进行验证时将所有这些注释聚合到一个单独的注释,比如注释聚合,即我可以将@NotNull和@Size从(javax.validation.constraints)聚合到以下注释"Name" .

 package org.StudentLib.CustomeAnnotations;
 import …
 @Target( {FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
 @Retention(RUNTIME)
 @Constraint(validatedBy = {})
 @Documented
 @NotNull
 @Id 
 @Size(message = "The size of the name should be between {min} and {max} caracters", 
                min = 1, max = 50, 
                payload = Severity.Info.class
                )
 public @interface Name {
    }
Run Code Online (Sandbox Code Playgroud)

那么我如何对持久性注释做同样的事情,我总是得到

此位置不允许使用注释@Id

为什么我收到此错误?有没有办法将持久性注释和验证注释组合在一个注释中.我问这个的原因是因为我的代码中有大约40个表(实体),每次我需要定义该表的主键时,我觉得我是代码重复.

nic*_*ild 1

您收到此错误是因为Target应用于注释类型的元注释在其值字段中Id不包含参数。ANNOTATION_TYPE查看 javadoc 您将看到它(ctrl+ f'@Target')。

@Target注释描述了java语言构造注释可以应用的内容。由于您无法将此注释应用于ANNOTATION_TYPE,因此您将无法创建此类快捷注释。

作为旁注,您可以通过查看此 javadoc 看到,其中@Target有一个@Targetof {ANNOTATION_TYPE},这实际上使注释成为元注释。