Meik Behnfeldt
Meik Behnfeldt

Reputation: 117

Is is possible to create a combo-annotation which combines multiple field annotations?

I have some boolean fields in my JPA entities which are annotated in the following way:

@Column(length = 1)
@Type(type = "yes_no")
private final boolean myField;

Is it possible to create a combo-annotation (e. g. MyAnnotation) which combines both of this annotations?

@MyAnnotation
private final boolean myField;

How?

Upvotes: 1

Views: 257

Answers (2)

Ondro Mihályi
Ondro Mihályi

Reputation: 7730

What you want is similar to CDI stereotypes - unfortunately, JPA does not support such a concept, therefore you must copy recurring annotations all over.

If you can afford to wrap your field into an object, you may mark it as @Embeddable and put your field into it - the annotations on that field will be copied wherever you embed that object into an entity. You may extend the annotations using @AnnotationOverrides. Obviously, the drawback of such solution is additional complexity when accessing the field in your entity.

Upvotes: 1

Neil Stockton
Neil Stockton

Reputation: 11531

Obviously you could create an annotation that provides the equivalent of multiple other annotations.

Equally obviously no JPA provider will support it, since they will check for the javax.persistence annotations only (not that @Type is javax.persistence).

Upvotes: 1

Related Questions