I have multiple classes where I have always the same annotations over the fields that define the primary key of the table for example:
@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;
Now I am looking for a way to aggregate all those annotations to one single annotation something like annotation aggregation when I do validation i.e. I can aggregate the @NotNull and @Size from (javax.validation.constraints ) to the following annotation called “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 {
}
So how do I do the same for persistent annotations, I always get
The annotation @Id is disallowed for this location
Why I am getting this error? Is there a way to combine the persistence annotations and validation annotation in one single annotation. The reason I am asking this is because I have around 40 tables( Entities ) in my code and I feel that I am dong code duplication every time I need to define the primary key of that table.
You are getting this error because the Target
meta-annotation applied to the Id
annotation type doesn't contain the parameters ANNOTATION_TYPE
in its value field. Take a look at the javadoc, and you will see it (ctrl+f '@Target').
The @Target
meta-annotation describes to what java language constructs an annotation can be applied. Due to the fact that you are unable to apply this annotation to an ANNOTATION_TYPE
, you will be unable to create such a shortcut annotation.
As a side note, you can see by looking at this javadoc, that @Target
has a @Target
of {ANNOTATION_TYPE}
, which is effectively what makes an annotation a meta-annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With