【发布时间】:2015-10-09 16:52:13
【问题描述】:
我的实体类有一个如下所示的列:
@Entity
@Table(name = "addons")
public class AddonsEntity {
private int id;
private String name;
private BigDecimal price;
private int addonGroupId;
private int order;
private AddonGroupsEntity addonGroupsByAddonGroupId;
@Id
@Column(name = "id", nullable = false, insertable = true, updatable = true)
@GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy="increment")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
这样转换成 sql 如下:
创建表格插件(id 整数不为空,....);
由于mysql中没有整数,所以它会抛出错误。
版本:
'org.hibernate', name: 'hibernate-core', version: '4.3.10.Final'
'org.hibernate', name: 'hibernate-c3p0', version: '4.3.10.Final'
'mysql', name: 'mysql-connector-java', version: '5.1.35'
Mysql Server version: 5.6.25 Homebrew
Java 1.8
SQL 翻译:
create table addons (
id integer not null,
addon_group_id integer not null,
name varchar(200) not null,
order integer not null,
price decimal(2,0) not null,
addonGroupsByAddonGroupId_id integer not null,
primary key (id)
)
错误:
2015-07-21 01:26:13 [] ERROR [Scanner-1] o.h.t.h.SchemaExport [SchemaExport.java:426] HHH000389: Unsuccessful: create table addons (id integer not null, addon_group_id integer not null, name varchar(200) not null, order integer not null, price decimal(2,0) not null, addonGroupsByAddonGroupId_id integer not null, primary key (id))
2015-07-21 01:26:13 [] ERROR [Scanner-1] o.h.t.h.SchemaExport [SchemaExport.java:427] 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 'order integer not null,price decimal(2,0) not null,addonGroups' at line 5
【问题讨论】:
-
请发布您遇到的错误?
-
..您希望在 DB 端使用哪种数据类型?
-
@Amogh:更新错误
-
实际上问题在于列名
order,ORDER是 MySQL 中的保留字,因此创建语句会失败。如果您正在使用注释,请尝试在AddonsEntity类中添加/修改注释@Column(name="[order]")。 -
如果您不想逐个更改属性,除了我的回答之外,您可以在休眠配置中设置
<property name="hibernate.globally_quoted_identifiers">true</property>。