JPA Eclipselink获得错误的序列号 [英] JPA Eclipselink getting wrong sequence number
问题描述
我正在使用一些映射到Oracle DB表的实体.对于ID生成,我使用的是以下注释的序列生成器:
I'm using some Entities mapped to Oracle DB-Tables. For ID-Generation I'm using a sequence generator annotated as following:
@Id
@SequenceGenerator(name = "SEQ_RULES", sequenceName = "SEQUENZ_RULES")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_RULES")
@Column(name = "SERIALNO")
protected Long serialno;
在执行程序期间,我从实体创建了一个新实例,并希望将其生成的实例持久化.重新启动数据库后,我通过JPA-EclipseLink而不是通过直接在数据库上的控制台获得了错误的序列号.
During programm execution I make a new Instance from my Entity and want to persist this generated one. After restart of the database I'm getting wrong sequence numbers through JPA-EclipseLink, but not through the Console directly on the database.
我打开了persistence.xml中的以下属性,以获取生成的语句中使用的绑定参数.
I turned on following properties in the persistence.xml toget also the binding parameters used in the generated statements.
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
例如:如果我生成一个新的实体实例并想要保留该实例,则得到2717的serialNo并执行
For example: If I generate a new instance of my entity and want to persist this one I get 2717 for serialNo and if I execute
SELECT SEQUENZ_RULES.NEXTVAL FROM DUAL
下一个我得到2767.问题在于,JPA生成的serialNo必须是唯一的,现在我仍然拥有一些带有该serialNo的数据集.我有一个例外:
I get 2767 as nextval. The problem is that the JPA-generated serialNo must be unique, and now I stil have some datasets with this serialNo. Im getting an exception:
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (JASON.SYS_C0084866) violated
非通过eclipse进行缓存是否会影响序列生成,或者这可能是什么错误?
Non Is there any caching through eclipse which is affecting the Sequence Generation or what could be the error for this?
使用的组件:
GlassFish 3.1.1
EclipseLink 2.3.0.v20110604-r9504
数据库:Oracle版本:Oracle Database 11g版本11.1.0.7.0-64bit
驱动程序:Oracle JDBC驱动程序版本:11.2.0.1.0
Used Components:
GlassFish 3.1.1
EclipseLink 2.3.0.v20110604-r9504
Database: Oracle Version: Oracle Database 11g Release 11.1.0.7.0 - 64bit
Driver: Oracle JDBC driver Version: 11.2.0.1.0
预先感谢
Adem
推荐答案
创建序列时,您指定了要增加的大小.例如,此序列增加1.
When you created the sequence you specified a size to increment by. For example this sequence increments by 1.
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
在JPA中使用SequenceGenerator
批注时,您可以指定分配大小.
When using the SequenceGenerator
annotation in JPA you can specify an allocation size.
@Id
@SequenceGenerator(name = "SEQ_RULES", sequenceName = "SEQUENZ_RULES",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_RULES")
@Column(name = "SERIALNO")
protected Long serialno;
确保JPA和Sequence DDL之间的分配大小和增量匹配.
Make sure the allocation size and the increment match between JPA and the Sequence DDL.
这篇关于JPA Eclipselink获得错误的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!