hibernate不会在mysql表上生成自动增量约束

时间:2012-03-16 07:44:45

标签: mysql hibernate auto-increment hibernate-annotations

我一直在不同的论坛上搜索问题并尝试了不同的解决方案,但我仍无法找到解决问题的正确答案。

我正在使用hibernate4注释来映射我的实体。一切正常,但在mysql中使用hibernate创建表时,没有检测到自动增量键。

我有以下代码:

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

使用hibernate它可以正常工作,id会自动分配给row,但在mysql表中它没有AutoIncrement Constraint。我必须手动将字段标记为AI。当我手动插入记录以进行测试或使用表的jdbc语句时,这会成为问题。 Plz让我知道我在配置中缺少什么阻止hibernate在相应的列上施加AutoIncrement Contraint。

1 个答案:

答案 0 :(得分:5)

使用IDENTITY生成器,并使用columnDefinition的{​​{1}}属性指定列的类型:

@Column
相关问题