简体   繁体   中英

Java annotation that summarizes other annotations

I would like to create an annotation for a JPA Entity field which summarizes some other annotations. This is the annotation I would like to create:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;

@Target(value = {ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Embedded
@AttributeOverrides({
        @AttributeOverride(name = "createdAt", column = @Column(name = "created_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")),
        @AttributeOverride(name = "lastChangedAt", column = @Column(name = "last_changed_at", columnDefinition = "TIMESTAMP WITH TIME ZONE")),
        @AttributeOverride(name = "lastChangedBy", column = @Column(name = "last_changed_by")),
        @AttributeOverride(name = "createdBy", column = @Column(name = "created_by")) })
public @interface EmbeddAdministrativeData {

}

But this gives a syntax error at @Embedded because the target of @Embedded is Field and Method. If I remove @Embedded and use it separately the @EmbeddAdministrativeData annotation is ignored. What would be the correct way to do this?

JPA 2.2 supports "meta-annotations" as per this link . Then it comes down to whether your JPA provider supports JPA 2.2 or not (the one I use, that these docs are for does, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM