简体   繁体   English

休眠:@GeneratedValue(strategy = GenerationType

[英]Hibernate: @GeneratedValue(strategy = GenerationType

I am using DB2 for my application. 我正在为我的应用程序使用DB2。 I run some insertion script after creating database. 创建数据库后,我运行一些插入脚本。 That insertion script generates records in table with id's given in insertion script. 该插入脚本在表中生成具有插入脚本中给定ID的记录。

Suppose for abc table insertion script creates a record with id = 3. As id's are set to auto generated in hibernate so while saving third record from application I got exception. 假设abc表插入脚本创建了一个id = 3的记录。由于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

I am using @GeneratedValue(strategy = GenerationType.AUTO) 我正在使用@GeneratedValue(strategy = GenerationType.AUTO)

What strategy = GenerationType I should use to overcome this problem. 我应该使用什么strategy = GenerationType来克服此问题。

There are issues with certain Databases and Hibernate when you use GenerationType.IDENTITY. 当您使用GenerationType.IDENTITY时,某些数据库和休眠状态会出现问题。 Try using a sequence and explicitlly configure everything for it: 尝试使用序列并为其明确配置所有内容:

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

对于DB2,@ @GeneratedValue(strategy = GenerationType.IDENTITY)应该可以正常工作。

If ids are provided in file then you don't need @GeneratedValue at all as there is no id to generate. 如果文件中提供了ID,则根本不需要@GeneratedValue ,因为没有ID可以生成。 And make sure to clean database as @SjB suggested. 并确保按照@SjB的建议清理数据库。

Also, without knowing much about DB2, the error message suggests that there may be other violation than just duplicate id on insert. 同样,在不了解DB2的情况下,错误消息表明可能存在其他违规行为,而不仅仅是插入时重复ID。 Are there any foreign keys involved? 是否有任何外键?

Nothing works except this query. 除此查询外,任何操作均无效。

alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;

DB2 should choose available ID itself but not doing so. DB2应该自己选择可用的ID,但不要这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate @GeneratedValue(strategy = GenerationType.AUTO) - Hibernate @GeneratedValue(strategy = GenerationType.AUTO) oracle上的Hibernate序列,@ GeneratedValue(strategy = GenerationType.AUTO) - Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO) 手动将数据库中的数据插入到具有 hibernate_sequence @GeneratedValue(strategy = GenerationType.TABLE) 的表中 - Manually insert Data from database in table which has hibernate_sequence @GeneratedValue(strategy = GenerationType.TABLE) 使用@GeneratedValue(strategy = GenerationType.AUTO)时,Hibernate不保存对象 - Hibernate does not save Object when using @GeneratedValue(strategy=GenerationType.AUTO) Hibernate中的GeneratedValue策略 - GeneratedValue strategy in hibernate Hibernate:oracle的strategy = GenerationType值 - Hibernate : strategy=GenerationType value for oracle JPA @GeneratedValue(strategy=GenerationType.AUTO) 在 MySQL 上不起作用 - JPA @GeneratedValue(strategy=GenerationType.AUTO) does not work on MySQL Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) 不起作用 - Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) not working 注释@Id 和@GeneratedValue(strategy = GenerationType.IDENTITY) 有什么用? 为什么世代类型是身份? - what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity? HIbernate Annotations @GeneratedValue 策略类型的区别 - HIbernate Annotations @GeneratedValue difference between the strategy type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM