问题
MySQL数据库中名为“ category”的表包含一个“ DESC ”关键字作为列名。
CREATE TABLE `category` (
`CATEGORY_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`NAME` varchar(10) NOT NULL,
`DESC` varchar(255) NOT NULL,
PRIMARY KEY (`CATEGORY_ID`) USING BTREE
);
Hibernate XML映射文件
<hibernate-mapping>
<class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb">
...
<property name="desc" type="string">
<column name="DESC" not-null="true" />
</property>
...
</class>
</hibernate-mapping>
或Hibernate批注
@Column(name = "DESC", nullable = false)
public String getDesc() {
return this.desc;
}
插入类别表时,出现以下错误消息:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
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 'DESC)
values ('CONSUMER', 'CONSUMER COMPANY')' at line 1
... 35 more
解
在Hibernate中,要将其插入“关键字”列名称中,应使用“ [[column name] ””这样的名称将其括起来。
Hibernate XML映射文件
<hibernate-mapping>
<class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb">
...
<property name="desc" type="string">
<column name="[DESC]" not-null="true" />
</property>
...
</class>
</hibernate-mapping>
或Hibernate批注
@Column(name = "[DESC]", nullable = false)
public String getDesc() {
return this.desc;
}
翻译自: https://mkyong.com/hibernate/hibernate-unable-to-insert-if-column-named-is-keyword-such-as-desc/