我在我的应用程序中使用DB2。我在创建数据库后运行了一些插入脚本。该插入脚本使用插入脚本中给定的id在表中生成记录。
假设abc表插入脚本创建了一条id = 3的记录,因为在hibernate中id被设置为自动生成,所以当保存应用程序中的第三条记录时,我得到了异常。
Caused by: com.ibm.websphere.ce.cm.DuplicateKeyException: One or
more values in the INSERT statement, UPDATE statement, or foreign
key update caused by a DELETE statement are not valid
because the primary key, unique constraint or unique
index identified by "1" constrains table我正在使用@GeneratedValue(strategy = GenerationType.AUTO)
我应该使用什么strategy = GenerationType来解决这个问题。
发布于 2011-10-21 15:38:59
除了这个查询,什么都不起作用。
alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;DB2应该自己选择可用的ID,而不是这样做。
发布于 2011-10-13 05:31:07
当您使用GenerationType.IDENTITY时,某些数据库和Hibernate会出现问题。尝试使用序列并明确地为其配置所有内容:
@Id
@SequenceGenerator(name = "DEPARTMENT_ID_GENERATOR", sequenceName="department_sequence", allocationSize=100)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "DEPARTMENT_ID_GENERATOR")
@Column(unique = true, nullable = false)
protected Long id;发布于 2011-10-12 18:35:57
对于DB2,@GeneratedValue(strategy = GenerationType.IDENTITY)应该可以正常工作。
https://stackoverflow.com/questions/7738710
复制相似问题