我如何在休眠@Formula中使用保留的table列
How do i use reserved table column in hibernate @Formula
我正在尝试使用在休眠中作为保留关键字的列名。
即使有引号,我仍然会出错。
我需要使用@Formula 创建一个子查询。
Table 如下所示,请注意名为 "row" 的列,我们正在从外部系统查询它并且无法更改。这就是问题的根源。
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`)
);
实体 class 看起来像这样:
@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 在现实世界中使用了 20 多个列)
在尝试获取 calculatedSum 的值时出现错误:
SQL 语句 "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 ?";预计 "identifier";
这里的问题是,由于 row 是保留关键字,查询最终会给出语法错误(在 o.row 中)。
这就是您认为在其周围添加引号将是解决方案的地方。
@Formula("select o.column1 + o.column2" +
" from MyTestTable o where `o.row` = `row` AND o.id = id")
private int calculatedSum;
但这会产生另一个错误:
16:18:30.547 [main] 错误 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 未找到列 "mytesttabl0_.o.row"; SQL声明:
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]
o.row 现在有一个额外的别名作为前缀,给我 mytesttabl0_。o.row
应该是 o.row
在这种情况下,如何编写对 MyTestTable.row 的工作引用?
根据 接受的答案,出现在 @Formula
中的内容必须是有效的 SQL 片段,并且不可能引用 class 中的任何字段;您只能参考 SQL 列和表格。假设您只想让 Hibernate 用 column1
和 column2
的总和填充 calculatedSum
字段,您应该使用:
@Formula(" column1 + column2 ")
private int calculatedSum;
我正在尝试使用在休眠中作为保留关键字的列名。 即使有引号,我仍然会出错。
我需要使用@Formula 创建一个子查询。
Table 如下所示,请注意名为 "row" 的列,我们正在从外部系统查询它并且无法更改。这就是问题的根源。
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`)
);
实体 class 看起来像这样:
@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 在现实世界中使用了 20 多个列)
在尝试获取 calculatedSum 的值时出现错误:
SQL 语句 "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 ?";预计 "identifier";
这里的问题是,由于 row 是保留关键字,查询最终会给出语法错误(在 o.row 中)。 这就是您认为在其周围添加引号将是解决方案的地方。
@Formula("select o.column1 + o.column2" +
" from MyTestTable o where `o.row` = `row` AND o.id = id")
private int calculatedSum;
但这会产生另一个错误:
16:18:30.547 [main] 错误 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 未找到列 "mytesttabl0_.o.row"; SQL声明: 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]
o.row 现在有一个额外的别名作为前缀,给我 mytesttabl0_。o.row 应该是 o.row
在这种情况下,如何编写对 MyTestTable.row 的工作引用?
根据 @Formula
中的内容必须是有效的 SQL 片段,并且不可能引用 class 中的任何字段;您只能参考 SQL 列和表格。假设您只想让 Hibernate 用 column1
和 column2
的总和填充 calculatedSum
字段,您应该使用:
@Formula(" column1 + column2 ")
private int calculatedSum;