(转) hibernate中使用数据库关键字(保留字)

 在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....

例如:

在MySql中,"desc"是保留字,我们有两种方式实现。

1,使用[]

<property name="desc" type="string" >
    <column name="[DESC]" length="255" not-null="true" />
</property>

注解

@Column(name = "[DESC]", nullable = false)
public String getDesc() {
return this.desc;
}

2,使用单引号包围双引号

<property name="desc" type="string" >
    <column name='"DESC"' length="255" not-null="true" />
</property>

注解

@Column(name = "\"DESC\"", nullable = false)
public String getDesc() {
return this.desc;
}

这种方式也适用于表名。

 
 

猜你喜欢

转载自cainiao1923.iteye.com/blog/2346241