注释聚合

  发布于 2023-01-29 18:33

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

@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;

现在我正在寻找一种方法,在我进行验证时将所有这些注释聚合到一个单独的注释,比如注释聚合,即我可以将@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 {
    }

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

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

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有