I'm trying to use column name that is a reserved keyword in hibernate. Even with quotes i'm still getting errors.
I need to create a subquery using @Formula.
Table looks like this below, notice the column named "row", we are querying this from an external system and this cannot be changed. This is the source of the problem.
CREATE TABLE MyTestTable (
id int,
`row` int NOT NULL,
column1 int NOT NULL DEFAULT 0,
column2 int NOT NULL DEFAULT 0,
PRIMARY KEY(id, `row`)
);
Entity class looks like this:
@Entity
public class MyTestTable implements Serializable {
@Id
private int id;
@Id
@Column(name = "[row]")
private int row;
@Formula("select o.column1 + o.column2" +
" from MyTestTable o where o.row = `row` AND o.id = id")
private int calculatedSum;
... getters and setters
}
(calculatedSum uses 20+ columns in the real world)
When trying to get the value of calculatedSum i get the error:
Syntax error in SQL statement "select select o.column1 + o.column2 from MyTestTable o where o.row[*] = mytesttabl0_.""row"" AND o.id = mytesttabl0_.id as col_0_0_ from MyTestTable mytesttabl0_ where mytesttabl0_.id=1 and mytesttabl0_.""row""=1 limit ?"; expected "identifier";
The problem here is that since row is a reserved keyword, the query ends up giving a syntax error (in o.row). This is the point where you would think that adding quotes around it would be the solution.
@Formula("select o.column1 + o.column2" +
" from MyTestTable o where `o.row` = `row` AND o.id = id")
private int calculatedSum;
But this gives another error:
16:18:30.547 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column "mytesttabl0_.o.row" not found; SQL statement: select select o.column1 + o.column2 from MyTestTable o where mytesttabl0_."o.row" = mytesttabl0_."row" AND o.id = mytesttabl0_.id as col_0_0_ from MyTestTable mytesttabl0_ where mytesttabl0_.id=1 and mytesttabl0_."row"=1 limit ? [42122-199]
The o.row has now been prefixed with an additional alias giving me mytesttabl0_.o.row Which should just be o.row
How do i write a working reference to MyTestTable.row in this scenario?