2

我的 Entity 类有一个如下所示的列:

@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 
4

2 回答 2

2

问题不在于integer. 问题出在 create 语句中。正如您在 create statement 中看到的那样,order列是用Mysql 中的保留字order integer not null,...where as创建的。ORDER

如果您使用注释,解决方案是

@Column(name = "[ORDER]", nullable = false)
    public int getOrder() {
        return this.order;
    }

或者

@Column(name = '"ORDER"', nullable = false)
    public int getOrder() {
        return this.order;
    }

如果您使用的是 hbm 文件,解决方案是:

   <property name="order">
            <column name="[ORDER]" not-null="true" />
   </property>

或者

<property name="order">
    <column name='"ORDER"' length="255" not-null="true" />
</property>
于 2015-07-20T20:23:47.487 回答
0

在我看来,您的应用程序未正确配置为使用MySQLDialect. 如何正确配置 Hibernate 取决于您执行它的环境,因此请查看正确的文档。

一个起点可能是堆栈溢出问题“为什么我需要配置数据源的 SQL 方言?

于 2015-07-20T20:16:15.573 回答