休眠不会在mysql表上生成自动增量约束 [英] hibernate not generate auto increment constraint on mysql table

查看:109
本文介绍了休眠不会在mysql表上生成自动增量约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过不同的论坛搜索信息,并且尝试过不同的解决方案,但仍然无法解决我的问题.

I have been searching through different forums for information and I have tried different solutions, but I'm still unable to correct my issue.

我正在使用hibernate4注释来映射我的实体.在mysql中使用休眠模式创建表时,未检测到自动增量键.

I am using hibernate4 annotations for mapping my entities. Auto increment key is not detected when tables are created using hibernate in mysql.

我有以下代码:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int responseId;

我也尝试过:

@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int responseId;

使用休眠模式时,id会自动分配给一行,但是在mysql表中,它没有AutoIncrement约束.我必须手动将字段标记为AI.当我手动插入记录进行测试或对该表使用jdbc语句时,这将成为问题.请让我知道我在配置中丢失了哪些内容,这些配置阻止了休眠ID分配自动增量约束.

With hibernate the id is automatically assigned to a row, but in the mysql table it has no AutoIncrement Constraint. I have to mark field as AI manually. This becomes problematic when I manually insert a record for testing or use jdbc statements for the table. Please let me know what I am missing in configuration that is preventing the hibernate id from assigning an AutoIncrement Contraint.

推荐答案

使用IDENTITY生成器,并使用@ColumncolumnDefinition属性指定列的类型:

Use the IDENTITY generator, and use the columnDefinition attribute of @Column to specify the type of the column:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "MEDIUMINT NOT NULL AUTO_INCREMENT")
private int responseId;

这篇关于休眠不会在mysql表上生成自动增量约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆