使用hibernate-commons-annotations
:
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.1.2.Final</version>
</dependency>
public final class Utils {
public static <T extends Annotation> T newAnnotation(Class<? extends Annotation> annotationType, Map<String, Object> annotationParams) {
var annotationDescriptor = new AnnotationDescriptor(annotationType);
annotationParams.forEach(annotationDescriptor::setValue);
return AnnotationFactory.create(annotationDescriptor);
}
}
var annotation = Utils.<Length>newAnnotation(Length.class, Map.of("min", 1, "max", 10));