做SSH整合的时候,总是出现错误信息:
类似这样:
: HHH000388: Unsuccessful: create table right (right_code varchar(255) not null auto_increment, right_desc varchar(255), right_name varchar(255), right_parent_code varchar(255), right_type varchar(255), right_url varchar(255), primary key (right_code)) ENGINE=InnoDB
九月 07, 2015 6:22:44 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: 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 'right (right_code varchar(255) not null auto_increment, right_desc varchar(255),' at line 1
我的表其中一个字段是:
/*
* 宝贝的描述
*/
@Column(name = "desc", nullable = false, length = 255)
private String desc;
错误是因为字段名和数据库关键字冲突了!!!
在网站找到的一些mysql关键字,注意避免将其用作字段名:
如果非要在hibernate中使用数据库关键字(保留字),解决方法可以是:
两种方式:
- 使用[]
<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;
}