I'm dealing with the necessity of using conditional compilation in a Java code, the classic problem of not having a #ifdef in Java while needing it. I found quite some different ways of doing so both here in SO as well as in other sites. Unfortunately it seems they either were too complicated, or they worked exclusivaly for code inside functions:
public void someFunction() {
//conditional code here
}
But my situation is a little different: I'm using Hybernate in Java to connect to a database. That database may be either Microsoft SQL or Oracle. I'm using ID fields that need to be increased when a new row is inserted by the webpage, but the configuration of this functionallity differ between Oracle and SQL. For example, in one such case, the code needed for SQL is
@GeneratedValue(strategy = GenerationType.AUTO)
While the one needed for oracle is:
@SequenceGenerator(name="SEQ_USUARIO_ID", sequenceName="SEQ_USUARIO_ID", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_USUARIO_ID")
This is code placed outside function code, in fact, right before it (example for Oracle):
@Id
@SequenceGenerator(name="SEQ_USUARIO_ID", sequenceName="SEQ_USUARIO_ID", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_USUARIO_ID")
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getId() {
return this.id;
}
For now, my default working code is for SQL, so whenever I have to 'recompile' for Oracle, I have to manually comment the SQL code and uncomment the Oracle one. This means a little of work since I have many tables in my database. I'ld like to minimize my work by putting this @ configuration in some form of "conditional compiling". In Java how could I do this?