在Hibernate中,当您尝试使用任何数据库保留关键字作为列名将对象保存到表中时,可能会遇到以下错误……
ERROR JDBCExceptionReporter:78 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'Datadabase reserved keyword....
保留关键字“ DESC”
在MySQL中,“ DESC”是保留关键字。 让我们看一些示例来演示如何在Hibernate中使用此保留关键字。
Hibernate XML映射文件
这是表列的默认XML映射文件实现,它将导致JDBCException…
<property name="desc" type="string" >
<column name="DESC" length="255" not-null="true" />
</property>
解
1.将关键字括在方括号[]中。
<property name="desc" type="string" >
<column name="[DESC]" length="255" not-null="true" />
</property>
2.使用单引号(')将双引号(“)括起来
<property name="desc" type="string" >
<column name='"DESC"' length="255" not-null="true" />
</property>
休眠注释
这是表列的默认注释实现,它将导致JDBCException…
@Column(name = "DESC", nullable = false)
public String getDesc() {
return this.desc;
}
解
1.将关键字括在方括号[]中。
@Column(name = "[DESC]", nullable = false)
public String getDesc() {
return this.desc;
}
2.用双引号(“)括起来。
@Column(name = "\"DESC\"", nullable = false)
public String getDesc() {
return this.desc;
}
结论
相同的解决方案也可以应用于保留关键字作为表名。
翻译自: https://mkyong.com/hibernate/how-to-use-database-reserved-keyword-in-hibernate/