与战略@GeneratedValue = GenerationType.AUTO产生重启后重复值 [英] @GeneratedValue with strategy=GenerationType.AUTO generates repeated value after restart

查看:2227
本文介绍了与战略@GeneratedValue = GenerationType.AUTO产生重启后重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID一个Hibernate实体配置为

I have a hibernate entity with an ID configured as

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

新元素的创建在第一次运行时工作正常。但是,如果我重新启动我的应用程序和检索回的记录,下一次我试着坚持这个实体,Hibernate试图使用没有重新启动应用程序时产生相同的ID。

The creation of new elements works ok in the first run. But if I restart my application and retrieve back the records, the next time I try to persist this entity, hibernate tries to use the same ID generated when the application was not restarted.

我得到下面的错误,并跟踪选项上运行时,我能够看到ID已被重用

I get the error below, and when running with trace option, I was able to see that the ID was being reused

*休眠:(?,?,?)插入org_myEntity(entitiyJID,entitityName,ID)值org.hibernate.util.JDBCExceptionReporter结果
  SQL错误:20000,SQLSTATE:23505
  org.hibernate.util.JDBCExceptionReporter的
  语句已放弃,因为它会导致重复键
  在一个独特的或主键约束或唯一索引值标识
  通过'TABLE_NAME'定义'SQL120725164357680。
  org.hibernate.event.def.AbstractFlushingEventListener结果
  无法同步与会话数据库状态
  org.hibernate.exception.ConstraintViolationException:不能*

*Hibernate: insert into org_myEntity (entitiyJID, entitityName, id) values (?, ?, ?) org.hibernate.util.JDBCExceptionReporter
SQL Error: 20000, SQLState: 23505 org.hibernate.util.JDBCExceptionReporter The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL120725164357680' defined on 'TABLE_NAME'. org.hibernate.event.def.AbstractFlushingEventListener
Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not*

顺便说一句,我使用Hibernate 3.3.2.GA,javax.persistance 2.0.0和德比10.5.1数据库

By the way, I am using hibernate 3.3.2.GA, javax.persistance 2.0.0 and Derby 10.5.1 database

是否有人有任何想法可能是错了我这一代,我怎么能解决这个问题?

Does someone have any idea what could be wrong on my generation and how could I fix it?

推荐答案

如果您使用AUTO,Hibernate会选择策略,以生成您的ID之一。从参考:

If you use AUTO, Hibernate will choose one of the strategies to generate your id. From the Reference:

AUTO - 无论是标识列,序列或台视
  底层数据库。

AUTO - either identity column, sequence or table depending on the underlying DB.

所以,你必须看到正在生成的ID,看德比正在使用的策略。虽然看起来很像,它复位重新启动您的应用程序生成器每次。尝试设置

So you have to see the ids being generated to see which strategy Derby is using. Although it looks like, it resets the generator everytime you restart your app. Try setting

<prop key="hibernate.hbm2ddl.auto">update</prop>

您可以使用序列发生器虽然很快修复它。这样的:

You could quickly fix it using a sequence generator though. Like:

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_seq_gen")
@SequenceGenerator(name="my_seq_gen", sequenceName="ENTITY_SEQ")
private Long id;

在哪里ENTITY_SEQ是在数据库中的序列的名称(您手动创建一个)。

Where ENTITY_SEQ is the name of the sequence in your database (you create one manually).

这篇关于与战略@GeneratedValue = GenerationType.AUTO产生重启后重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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