Search code examples
javaannotations

Conditional Annotations based on environment


Is it possible to have Java annotations that are applied conditionally? For example, lets say I have a hibernate mapping with an annotated sequence:

@Id
@Column(name = "TABLE_ID")
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
@SequenceGenerator(name = "generator", sequenceName = "TABLE_SEQ")
public Long getId() {
    return this.Id;
}

Is it possible to do something like the following, where the annotations are removed based on, say, an environment variable?

@Id
@Column(name = "TABLE_ID")
if(env.equals('dev')){
  @GeneratedValue(strategy = SEQUENCE, generator = "generator")
  @SequenceGenerator(name = "generator", sequenceName = "TABLE_SEQ")}
public Long getId() {
    return this.Id;
}

NOTE: I do understand that in this scenario can use *.hbm.xml files for different environments, however I would like to use annotations as there are less files to maintain.


Solution

  • Short answer, no it's not. Medium answer, you could use an annotation processor to do the job, but you then have to maintain an annotation processor..