• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change the default value of an annotation attribute?  RSS feed

 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel

In the annotation code itself, can you not provide a default value for the name attribute (i.e. "street") so that if it is not provided, this is the value set.





Edit: Apologies Roel, I've just read your post again and I don't think my answer provides any help here.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

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

Kind regards,
Roel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!