【问题标题】:Hibernate converting java int to mysql integerHibernate将java int转换为mysql整数
【发布时间】: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:更新错误
  • 实际上问题在于列名orderORDER 是 MySQL 中的保留字,因此创建语句会失败。如果您正在使用注释,请尝试在AddonsEntity 类中添加/修改注释@Column(name="[order]")
  • 如果您不想逐个更改属性,除了我的回答之外,您可以在休眠配置中设置<property name="hibernate.globally_quoted_identifiers">true</property>

标签: mysql hibernate dialect


【解决方案1】:

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

如果你使用注解,解决方法是

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

【讨论】:

  • 除此之外,如果您不想逐个更改属性,您可以设置&lt;property name="hibernate.globally_quoted_identifiers" value="true"/&gt;
【解决方案2】:

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

这可能是堆栈溢出问题“Why do I need to configure the SQL dialect of a data source?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2017-08-21
    • 2013-10-16
    • 2021-09-23
    • 2011-01-22
    • 2023-04-09
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多