Reputation: 2612
Issue
We build up an intern java spring framework which we use in two different projects. One project is using OracleDB and the other project is using MS SQL.
When it comes to reserved keywords MS SQL is complaining about key
.
So we fixed the column name by escaping:
public class MyEntity {
@Column(name = "\"key\"")
private String key;
private String foo;
But now it is not working in the oracle db anymore because the generated sql contains now the escape characters:
select myEntity."key", myEntity.foo from myEntity
(java.sql.SQLSyntaxErrorException: ORA-00904: "myEntity."key": invalid identifier")
Question
Upvotes: 0
Views: 827
Reputation: 10716
The problem here is that double-quoting an identifier in Oracle makes it case-sensitive. If the Oracle schema has been created without the quoted column definition, then by default, the created column name is uppercase.
Since, on the other hand MSSQL is case-insensitive by default, I suspect @Column(name = "\"KEY\"")
will do the trick (casing will be ignored by MSSQL, while the uppercase name will match Oracle's column definition).
Is it possible to have 2 javax.persistence.Column annotations on one attribute depending on db dialect or maybe some external property?
You can override the column annotation using XML entity mappings.
Is it somehow possible to write own annotation and than differentiate both cases? (I suppose Entity scan would not consider own annotation)
Nope. Maybe you could implement a custom PhysicalNamingStrategy
, though.
Upvotes: 1