I was wondering if it's possible to somehow set/change the default value of an annotation attribute. The example uses a jpa annotation, but could be any annotation.
You have the @AttributeOverride annotation which has 2 required properties: name and column. The annotation in action (in a simplified version of the actual problem):
If you have 15 adresses, you'll have 15 duplicates of name="street". It would be nice if you could create a custom @AttributeStreetOverride annotation which can be used in place of the @AttributeOverride annotation without having to define the name attribute each time (the DRY-principle). So the above code would be something like:
From what I so far found on google I don't think it's possible and the best I've got is to define some constant values and use them as the value for the name attribute. But still hoping it can be done
Yeah unfortunately there is no inheritance in annotations. You cannot have AttributeStreetOverride extend from AttributeOverride. I don't completely understand the reasoning. Something about not having to load all the classes. I think there is a JSR out there to make attributes polymorphic. If you can extend the code that handles the annotation (or if the annotation handling was done in your own code), you can always add custom annotation handling code. Remember that annotations aren;t polymorphic, but annotation handling is done in Java which is theorotically extensible. You are at the mercy of the container, though. If the container doesn't allow you to extend it's annotation handling then you are screwed.
Jayesh A Lalwani wrote:If you can extend the code that handles the annotation (or if the annotation handling was done in your own code), you can always add custom annotation handling code.
Unfortunately it's the @AttributeOverride annotation I want to provide a default value for, so annotation handling is not done in our code And I also don't want to tweak the annotation handling code of Hibernate (our JPA implementation). So I just settled with creating a few string constant values and use them in the annotation attribute. Not the fancy stuff I was hoping for, but it does the job.
Like you already supposed, it's (unfortunately) not what I'm looking for. But your effort/suggestion is highly appreciated. I know I can set the default value of an annotation attribute, but that's not enough in my use case.
A little bit more detail about the use case: I have an Embeddable which is used in several other classes and thus I have to use @AttributeOverride and @AttributeOverrides to map the property to the appropriate column. In some classes I will have 30-40 of these Embeddable objects, so the name="street" will be repeated 30-40 times. If someone changes the Embeddable's property street to straat (Dutch translation) you have to change all these occurences. If you would have the @AttributeStreetOverride annotation, you simply had to change its definition and you were done. That's why I was looking for some kind of workaround. But all recources on the almighty internet have the same conclusion: no annotation inheritance
Maybe it could be possible to define my own custom annotations and tell the JPA provider it should handle these annotations as @AttributeOverride ones