Discussion:
Error in EntityManager.persist () if the database already contains a row with id=0
Krishnaprasad Subbarao
2013-02-18 19:27:32 UTC
Permalink
Hello,

Following error occurs while I am trying to persist a record. The id field
has been assigned a default value (0) as per the guidelines.

<openjpa-2.2.1-r422266:1396819 fatal store error>
org.apache.openjpa.persistence.RollbackException: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
FailedObject: ***@7aa47aa4
at org.apache.openjpa.persistence.EntityManagerImpl.commit(
EntityManagerImpl.java:594)
at com.ibm.test.jpa.JPAAddEntityTest.main(JPAAddEntityTest.java:38
)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error>
org.apache.openjpa.persistence.EntityExistsException: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
FailedObject: ***@7aa47aa4
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(
BrokerImpl.java:5080)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(
BrokerImpl.java:2653)
at org.apache.openjpa.kernel.BrokerImpl.persist(
BrokerImpl.java:2573)
at org.apache.openjpa.kernel.BrokerImpl.persist(
BrokerImpl.java:2556)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(
SingleFieldManager.java:800)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(
SingleFieldManager.java:621)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(
SingleFieldManager.java:589)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(
SingleFieldManager.java:505)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(
StateManagerImpl.java:3028)
at org.apache.openjpa.kernel.PNewState.beforeFlush(
PNewState.java:44)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(
StateManagerImpl.java:1042)
at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2114
)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(
BrokerImpl.java:2074)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(
BrokerImpl.java:1992)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(
LocalManagedRuntime.java:81)
at org.apache.openjpa.kernel.BrokerImpl.commit(
BrokerImpl.java:1516)
at org.apache.openjpa.kernel.DelegatingBroker.commit(
DelegatingBroker.java:933)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(
EntityManagerImpl.java:570)
... 1 more

The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A record
with Primary Key value = 0 already exists, so duplicate primary key check
fails.

This zip file contains a sample code with which I was able to reproduce
this error.

Adding a row with id=0 before persisting any object throws the error
mentioned above.

I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this row
in the database.

Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.



Thanks and Regards,

KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Rick Curtis
2013-02-18 19:58:35 UTC
Permalink
Could you try adding an @Version field to your Entities?

Thanks,
Rick


On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id field
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A record
with Primary Key value = 0 already exists, so duplicate primary key check
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this row
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Krishnaprasad Subbarao
2013-02-19 08:50:46 UTC
Permalink
Attaching a smaller size example to due attachment size issues.

I am using OpenJPA 2.2.1. I request you to copy the openjpa-all-2.2.1.jar
and the database driver in to the lib folder before executing the sample.


Thanks and Regards,

KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab


Phone: 91-20-40117370 | Mobile: 91--9096009911
E-mail: krishnaprasads-xthvdsQ13ZrQT0dZR+***@public.gmane.org
Chat: krishnaprasad.su-***@public.gmane.org
Find me on: and within IBM on:


Tech Park One (panchshill)
Pune, MH 411006
India

----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----

From: Krishnaprasad Subbarao/India/IBM
To: users-8spYzHhHo/***@public.gmane.org,
Cc: users <users-8spYzHhHo/***@public.gmane.org>
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0


Hello Rick/Kevin,

Thanks for your suggestions.

The mapping for com.ibm.wdp.bss.party.entity.PartyObject is defined in
party.orm.xml. This contains a mapping <version name="version"/> for
version field used for optimistic locking.

Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below

<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1"/>
</id>

[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.

The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.

I have following assumptions and questions about this issue.

Defining version in the .orm.xml file will not make any difference as
compared to the @version annotation specified in the Bean
Can we configure OpenJPA to use a different default value for Identity
fields say -1 instead of the Java default value 0. A new object to be
persisted in the database will have id attribute set to -1 instead of 0?
Can we configure OpenJPA to ignore the database row with Primary Key 0?


Thanks and Regards,

KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab


Phone: 91-20-40117370 | Mobile: 91--9096009911
E-mail: krishnaprasads-xthvdsQ13ZrQT0dZR+***@public.gmane.org
Chat: krishnaprasad.su-***@public.gmane.org
Find me on: and within IBM on:


Tech Park One (panchshill)
Pune, MH 411006
India




From: Rick Curtis <curtisr7-***@public.gmane.org>
To: users <users-8spYzHhHo/***@public.gmane.org>,
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0



Could you try adding an @Version field to your Entities?

Thanks,
Rick


On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key
check
Post by Krishnaprasad Subbarao
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this
row
Post by Krishnaprasad Subbarao
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Kevin Sutter
2013-02-19 16:31:16 UTC
Permalink
Are you indicating that if you remove the PART record with an id of 0 from
the database, then everything works just fine? But, if you leave this
initial PART record with an id of 0, then you get the duplicate key
exception? If that's the case and this PART with an id of 0 has to be part
of the dataset, then why not just start the id generation with a larger
number? (That's what Albert's suggestion was, but he only replied to the
dev list. BTW, if you post to multiple lists, then you need to monitor
multiple lists. It's just part of the game. Thanks.)

Kevin

On Tue, Feb 19, 2013 at 2:50 AM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Attaching a smaller size example to due attachment size issues.
I am using OpenJPA 2.2.1. I request you to copy the openjpa-all-2.2.1.jar
and the database driver in to the lib folder before executing the sample.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
------------------------------
[image: 2D barcode - encoded with contact information] *Phone:*91-20-40117370|
*Mobile:* 91--9096009911*
http://www.linkedin.com/in/krishnaprasadsubbarao]<http://www.linkedin.com/in/krishnaprasadsubbarao>
https://w3-connections.ibm.com/profiles/html/profileView.do?key=af2eda97-7154-43de-9565-cd3a3213fc31&lang=en_us]<https://w3-connections.ibm.com/profiles/html/profileView.do?key=af2eda97-7154-43de-9565-cd3a3213fc31&lang=en_us>
[image: IBM]
Tech Park One (panchshill)
Pune, MH 411006
India
----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----
From: Krishnaprasad Subbarao/India/IBM
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
------------------------------
Hello Rick/Kevin,
Thanks for your suggestions.
The mapping for *com.ibm.wdp.bss.party.entity.PartyObject* is defined in
party.orm.xml. This contains a mapping <version name=*"version"*/>for version field used for optimistic locking.
Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below
<id name=*"id"*>
<column name=*"Id"*/>
<generated-value strategy=*"SEQUENCE"* generator=*"PART_SEQ"*/>
<sequence-generator name=*"PART_SEQ"* sequence-name=*
"PART_SEQ"* allocation-size=*"1"*/>
</id>
[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.
The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.
I have following assumptions and questions about this issue.
1. Defining version in the .orm.xml file will not make any difference
2. Can we configure OpenJPA to use a different default value for
Identity fields say -1 instead of the Java default value 0. A new object to
be persisted in the database will have id attribute set to -1 instead of 0?
3. Can we configure OpenJPA to ignore the database row with Primary
Key 0?
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
------------------------------
[image: 2D barcode - encoded with contact information] *Phone:*91-20-40117370|
*Mobile:* 91--9096009911*
http://www.linkedin.com/in/krishnaprasadsubbarao]<http://www.linkedin.com/in/krishnaprasadsubbarao>
https://w3-connections.ibm.com/profiles/html/profileView.do?key=af2eda97-7154-43de-9565-cd3a3213fc31&lang=en_us]<https://w3-connections.ibm.com/profiles/html/profileView.do?key=af2eda97-7154-43de-9565-cd3a3213fc31&lang=en_us>
[image: IBM]
Tech Park One (panchshill)
Pune, MH 411006
India
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
------------------------------
Thanks,
Rick
On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key check
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this row
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Krishnaprasad Subbarao
2013-02-20 13:38:17 UTC
Permalink
Hello Kevin/Albert,

Thanks for suggestions. I added initial-value attribute to the sequence
generator mapping in the test program attached earlier, but the same
exception is thrown.

The new sequence generator mapping is as follows,

<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1" initial-value="100"/>
</id>

The initial-value 100 specified in the mapping is not present in the
database.

I am not sure if I understand this issue well, but it seems OpenJPA is
checking for duplicate id before actually reading a value from the given
sequence and populating it in the object to be persisted. When the
duplicate check is done, the id field has default value "0", the database
already has a row with id=0, so the duplicate check fails. When there is
no row in the database with id=0 the duplicate check succeeds, after that,
the sequence is used to populate the correct id value before persisting
the record.

By removing the database row with id=0, a row was successfully added in
the database of this example as well as in my application. But since we
have business logic around rows with id=0, we wish to avoid changing the
business logic and keep it as a last option.


Thanks and Regards,

KRISHNAPRASAD SUBBARAO




From: Kevin Sutter <kwsutter-***@public.gmane.org>
To: users-8spYzHhHo/***@public.gmane.org,
Date: 02/19/2013 11:32 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0



Are you indicating that if you remove the PART record with an id of 0 from
the database, then everything works just fine? But, if you leave this
initial PART record with an id of 0, then you get the duplicate key
exception? If that's the case and this PART with an id of 0 has to be
part of the dataset, then why not just start the id generation with a
larger number? (That's what Albert's suggestion was, but he only replied
to the dev list. BTW, if you post to multiple lists, then you need to
monitor multiple lists. It's just part of the game. Thanks.)

Kevin

On Tue, Feb 19, 2013 at 2:50 AM, Krishnaprasad Subbarao <
krishnaprasads-xthvdsQ13ZrQT0dZR+***@public.gmane.org> wrote:
Attaching a smaller size example to due attachment size issues.

I am using OpenJPA 2.2.1. I request you to copy the openjpa-all-2.2.1.jar
and the database driver in to the lib folder before executing the sample.


Thanks and Regards,

KRISHNAPRASAD SUBBARAO

Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab


Phone: 91-20-40117370 | Mobile: 91--9096009911
E-mail: krishnaprasads-xthvdsQ13ZrQT0dZR+***@public.gmane.org
Chat: krishnaprasad.su-***@public.gmane.org
Find me on: and within IBM on:


Tech Park One (panchshill)
Pune, MH 411006
India


----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----

From: Krishnaprasad Subbarao/India/IBM
To: users-8spYzHhHo/***@public.gmane.org,
Cc: users <users-8spYzHhHo/***@public.gmane.org>
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0


Hello Rick/Kevin,

Thanks for your suggestions.

The mapping for com.ibm.wdp.bss.party.entity.PartyObject is defined in
party.orm.xml. This contains a mapping <version name="version"/> for
version field used for optimistic locking.

Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below

<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1"/>
</id>

[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.

The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.

I have following assumptions and questions about this issue.
1. Defining version in the .orm.xml file will not make any difference
as compared to the @version annotation specified in the Bean
2. Can we configure OpenJPA to use a different default value for
Identity fields say -1 instead of the Java default value 0. A new object
to be persisted in the database will have id attribute set to -1 instead
of 0?
3. Can we configure OpenJPA to ignore the database row with Primary
Key 0?


Thanks and Regards,

KRISHNAPRASAD SUBBARAO

Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab


Phone: 91-20-40117370 | Mobile: 91--9096009911
E-mail: krishnaprasads-xthvdsQ13ZrQT0dZR+***@public.gmane.org
Chat: krishnaprasad.su-***@public.gmane.org
Find me on: and within IBM on:


Tech Park One (panchshill)
Pune, MH 411006
India





From: Rick Curtis <curtisr7-***@public.gmane.org>
To: users <users-8spYzHhHo/***@public.gmane.org>,
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0



Could you try adding an @Version field to your Entities?

Thanks,
Rick


On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key check
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this row
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Boblitz John
2013-02-20 15:08:52 UTC
Permalink
Hello,

I remember having a similar problem when I first started my current project 2 years ago.

I found that if I assigned the id / version fields with zero, I would get an error. However,
if I checked the value of these fields and made the assignment only when they were NOT zero
jpa accepted them! IOW - JPA seemed to be aware of whether the fields were assigned a default
value from the JVM or whether I specifically did so. If I did, jpa wanted to use my values, if the JVM did
then jpa generates the value while persisting.

tl;dr

try creating a new Object and only populate the id / version field if the value is NOT zero.

John
-----Original Message-----
Sent: Wednesday, February 20, 2013 2:38 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database already
contains a row with id=0
Hello Kevin/Albert,
Thanks for suggestions. I added initial-value attribute to the sequence
generator mapping in the test program attached earlier, but the same
exception is thrown.
The new sequence generator mapping is as follows,
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1" initial-value="100"/>
</id>
The initial-value 100 specified in the mapping is not present in the database.
I am not sure if I understand this issue well, but it seems OpenJPA is checking
for duplicate id before actually reading a value from the given sequence and
populating it in the object to be persisted. When the duplicate check is done,
the id field has default value "0", the database already has a row with id=0, so
the duplicate check fails. When there is no row in the database with id=0 the
duplicate check succeeds, after that, the sequence is used to populate the
correct id value before persisting the record.
By removing the database row with id=0, a row was successfully added in the
database of this example as well as in my application. But since we have
business logic around rows with id=0, we wish to avoid changing the business
logic and keep it as a last option.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Date: 02/19/2013 11:32 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0
Are you indicating that if you remove the PART record with an id of 0 from
the database, then everything works just fine? But, if you leave this initial
PART record with an id of 0, then you get the duplicate key exception? If
that's the case and this PART with an id of 0 has to be part of the dataset,
then why not just start the id generation with a larger number? (That's what
Albert's suggestion was, but he only replied to the dev list. BTW, if you post
to multiple lists, then you need to monitor multiple lists. It's just part of the
game. Thanks.)
Kevin
On Tue, Feb 19, 2013 at 2:50 AM, Krishnaprasad Subbarao <
Attaching a smaller size example to due attachment size issues.
I am using OpenJPA 2.2.1. I request you to copy the openjpa-all-2.2.1.jar and
the database driver in to the lib folder before executing the sample.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----
From: Krishnaprasad Subbarao/India/IBM
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Hello Rick/Kevin,
Thanks for your suggestions.
The mapping for com.ibm.wdp.bss.party.entity.PartyObject is defined in
party.orm.xml. This contains a mapping <version name="version"/> for
version field used for optimistic locking.
Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1"/>
</id>
[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.
The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.
I have following assumptions and questions about this issue.
1. Defining version in the .orm.xml file will not make any difference
2. Can we configure OpenJPA to use a different default value for
Identity fields say -1 instead of the Java default value 0. A new object
to be persisted in the database will have id attribute set to -1 instead
of 0?
3. Can we configure OpenJPA to ignore the database row with Primary
Key 0?
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Thanks,
Rick
On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key
check
Post by Krishnaprasad Subbarao
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this
row
Post by Krishnaprasad Subbarao
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Krishnaprasad Subbarao
2013-02-20 17:32:37 UTC
Permalink
Hello,

Thanks for updates. I just confirmed that I am not explicitly setting id
fields to 0. Still getting the same error for the sample application
attached below.

This sample contains database schema SQL, orm mapping files and Java
classes. I am getting the error with one row added to the database with id
=0. This app works well if I DELETE the row from database.

I request you to let me know if I have done anything incorrect.


Thanks and Regards,

KRISHNAPRASAD SUBBARAO



From: Boblitz John <John.Boblitz-m86EuDhUrFFWk0Htik3J/***@public.gmane.org>
To: "'users-8spYzHhHo/***@public.gmane.org'" <users-8spYzHhHo/***@public.gmane.org>,
Date: 02/20/2013 08:38 PM
Subject: RE: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0



Hello,

I remember having a similar problem when I first started my current
project 2 years ago.

I found that if I assigned the id / version fields with zero, I would get
an error. However,
if I checked the value of these fields and made the assignment only when
they were NOT zero
jpa accepted them! IOW - JPA seemed to be aware of whether the fields
were assigned a default
value from the JVM or whether I specifically did so. If I did, jpa wanted
to use my values, if the JVM did
then jpa generates the value while persisting.

tl;dr

try creating a new Object and only populate the id / version field if the
value is NOT zero.

John
-----Original Message-----
Sent: Wednesday, February 20, 2013 2:38 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database already
contains a row with id=0
Hello Kevin/Albert,
Thanks for suggestions. I added initial-value attribute to the sequence
generator mapping in the test program attached earlier, but the same
exception is thrown.
The new sequence generator mapping is as follows,
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1" initial-value="100"/>
</id>
The initial-value 100 specified in the mapping is not present in the database.
I am not sure if I understand this issue well, but it seems OpenJPA is checking
for duplicate id before actually reading a value from the given sequence and
populating it in the object to be persisted. When the duplicate check is done,
the id field has default value "0", the database already has a row with id=0, so
the duplicate check fails. When there is no row in the database with id=0 the
duplicate check succeeds, after that, the sequence is used to populate the
correct id value before persisting the record.
By removing the database row with id=0, a row was successfully added in the
database of this example as well as in my application. But since we have
business logic around rows with id=0, we wish to avoid changing the business
logic and keep it as a last option.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Date: 02/19/2013 11:32 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0
Are you indicating that if you remove the PART record with an id of 0 from
the database, then everything works just fine? But, if you leave this initial
PART record with an id of 0, then you get the duplicate key exception?
If
that's the case and this PART with an id of 0 has to be part of the dataset,
then why not just start the id generation with a larger number? (That's what
Albert's suggestion was, but he only replied to the dev list. BTW, if you post
to multiple lists, then you need to monitor multiple lists. It's just part of the
game. Thanks.)
Kevin
On Tue, Feb 19, 2013 at 2:50 AM, Krishnaprasad Subbarao <
Attaching a smaller size example to due attachment size issues.
I am using OpenJPA 2.2.1. I request you to copy the
openjpa-all-2.2.1.jar and
the database driver in to the lib folder before executing the sample.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----
From: Krishnaprasad Subbarao/India/IBM
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Hello Rick/Kevin,
Thanks for your suggestions.
The mapping for com.ibm.wdp.bss.party.entity.PartyObject is defined in
party.orm.xml. This contains a mapping <version name="version"/> for
version field used for optimistic locking.
Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1"/>
</id>
[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.
The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.
I have following assumptions and questions about this issue.
1. Defining version in the .orm.xml file will not make any difference
2. Can we configure OpenJPA to use a different default value for
Identity fields say -1 instead of the Java default value 0. A new object
to be persisted in the database will have id attribute set to -1 instead
of 0?
3. Can we configure OpenJPA to ignore the database row with Primary
Key 0?
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Thanks,
Rick
On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key
check
Post by Krishnaprasad Subbarao
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this
row
Post by Krishnaprasad Subbarao
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
Krishnaprasad Subbarao
2013-02-23 10:32:57 UTC
Permalink
Hello,

I tried to few things but still not getting a solution for this. I am new
to JPA. I think I am missing something or have done something incorrect,
in the mapping orm.xml or in the beans but not able to find it myself.

I request all to review and try the attached sample which contains a
script to generate a small database and then execute the
JPAAddEntityTest.java

Thanks and Regards,

KRISHNAPRASAD SUBBARAO



From: Krishnaprasad Subbarao/India/***@IBMIN
To:
Cc: "'users-8spYzHhHo/***@public.gmane.org'" <users-8spYzHhHo/***@public.gmane.org>
Date: 02/20/2013 11:03 PM
Subject: RE: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0



Hello,

Thanks for updates. I just confirmed that I am not explicitly setting id
fields to 0. Still getting the same error for the sample application
attached below.

This sample contains database schema SQL, orm mapping files and Java
classes. I am getting the error with one row added to the database with id
=0. This app works well if I DELETE the row from database.

I request you to let me know if I have done anything incorrect.


Thanks and Regards,

KRISHNAPRASAD SUBBARAO



From: Boblitz John <John.Boblitz-m86EuDhUrFFWk0Htik3J/***@public.gmane.org>
To: "'users-8spYzHhHo/***@public.gmane.org'" <users-8spYzHhHo/***@public.gmane.org>,
Date: 02/20/2013 08:38 PM
Subject: RE: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0



Hello,

I remember having a similar problem when I first started my current
project 2 years ago.

I found that if I assigned the id / version fields with zero, I would get
an error. However,
if I checked the value of these fields and made the assignment only when
they were NOT zero
jpa accepted them! IOW - JPA seemed to be aware of whether the fields
were assigned a default
value from the JVM or whether I specifically did so. If I did, jpa wanted
to use my values, if the JVM did
then jpa generates the value while persisting.

tl;dr

try creating a new Object and only populate the id / version field if the
value is NOT zero.

John
-----Original Message-----
Sent: Wednesday, February 20, 2013 2:38 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database already
contains a row with id=0
Hello Kevin/Albert,
Thanks for suggestions. I added initial-value attribute to the sequence
generator mapping in the test program attached earlier, but the same
exception is thrown.
The new sequence generator mapping is as follows,
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1" initial-value="100"/>
</id>
The initial-value 100 specified in the mapping is not present in the database.
I am not sure if I understand this issue well, but it seems OpenJPA is checking
for duplicate id before actually reading a value from the given sequence and
populating it in the object to be persisted. When the duplicate check is done,
the id field has default value "0", the database already has a row with id=0, so
the duplicate check fails. When there is no row in the database with id=0 the
duplicate check succeeds, after that, the sequence is used to populate the
correct id value before persisting the record.
By removing the database row with id=0, a row was successfully added in the
database of this example as well as in my application. But since we have
business logic around rows with id=0, we wish to avoid changing the business
logic and keep it as a last option.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Date: 02/19/2013 11:32 PM
Subject: Re: Fw: Error in EntityManager.persist () if the database
already contains a row with id=0
Are you indicating that if you remove the PART record with an id of 0 from
the database, then everything works just fine? But, if you leave this initial
PART record with an id of 0, then you get the duplicate key exception?
If
that's the case and this PART with an id of 0 has to be part of the dataset,
then why not just start the id generation with a larger number? (That's what
Albert's suggestion was, but he only replied to the dev list. BTW, if you post
to multiple lists, then you need to monitor multiple lists. It's just part of the
game. Thanks.)
Kevin
On Tue, Feb 19, 2013 at 2:50 AM, Krishnaprasad Subbarao <
Attaching a smaller size example to due attachment size issues.
I am using OpenJPA 2.2.1. I request you to copy the
openjpa-all-2.2.1.jar and
the database driver in to the lib folder before executing the sample.
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
----- Forwarded by Krishnaprasad Subbarao/India/IBM on 02/19/2013 02:12 PM
-----
From: Krishnaprasad Subbarao/India/IBM
Date: 02/19/2013 01:54 PM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Hello Rick/Kevin,
Thanks for your suggestions.
The mapping for com.ibm.wdp.bss.party.entity.PartyObject is defined in
party.orm.xml. This contains a mapping <version name="version"/> for
version field used for optimistic locking.
Also, I have manually corrected the party.orm.xml to add correct mapping
for <id field. The mapping for Id field defines a sequence generator with
allocation-size=1 as defined below
<id name="id">
<column name="Id"/>
<generated-value strategy="SEQUENCE" generator="PART_SEQ"/>
<sequence-generator name="PART_SEQ" sequence-name=
"PART_SEQ" allocation-size="1"/>
</id>
[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM] The
attached sample contains a working sample to reproduce the error. The test
database contains a row with id 0 in the table PART. The execution of
JPAAddEntityTest.java fails with the error given in the first mail below.
The moment I remove the row with id 0 manually from table PART and
re-execute the JPAAddEntityTest.java. Records successfully get inserted in
to PART table and ADDRESS table with appropriate next values from the
sequences.
I have following assumptions and questions about this issue.
1. Defining version in the .orm.xml file will not make any difference
2. Can we configure OpenJPA to use a different default value for
Identity fields say -1 instead of the Java default value 0. A new object
to be persisted in the database will have id attribute set to -1 instead
of 0?
3. Can we configure OpenJPA to ignore the database row with Primary
Key 0?
Thanks and Regards,
KRISHNAPRASAD SUBBARAO
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
Phone: 91-20-40117370 | Mobile: 91--9096009911
Tech Park One (panchshill)
Pune, MH 411006
India
Date: 02/19/2013 01:28 AM
Subject: Re: Error in EntityManager.persist () if the database
already contains a row with id=0
Thanks,
Rick
On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id
field
Post by Krishnaprasad Subbarao
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists
in
Post by Krishnaprasad Subbarao
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A
record
Post by Krishnaprasad Subbarao
with Primary Key value = 0 already exists, so duplicate primary key
check
Post by Krishnaprasad Subbarao
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this
row
Post by Krishnaprasad Subbarao
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
*Rick Curtis*
[attachment "BSS-JPA.zip" deleted by Krishnaprasad Subbarao/India/IBM]
Kevin Sutter
2013-02-19 16:25:55 UTC
Permalink
Forgot the users mailing list on this suggestion...

---------- Forwarded message ----------
From: Albert Lee <allee8285-***@public.gmane.org>
Date: Mon, Feb 18, 2013 at 2:06 PM
Subject: Re: Error in EntityManager.persist () if the database already
contains a row with id=0
To: dev-8spYzHhHo/***@public.gmane.org


Check this out:

http://people.apache.org/~mikedd/nightly.builds/apache-openjpa-2.3.0-SNAPSHOT/docs/docbook/manual.html#jpa_overview_mapping_sequence

sequence-generator has attribute "int initialValue: The initial sequence
value."

Albert

On Mon, Feb 18, 2013 at 1:27 PM, Krishnaprasad Subbarao <
Post by Krishnaprasad Subbarao
Hello,
Following error occurs while I am trying to persist a record. The id field
has been assigned a default value (0) as per the guidelines.
<openjpa-2.2.1-r422266:1396819 fatal store error> *
org.apache.openjpa.persistence.RollbackException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:594*)
at com.ibm.test.jpa.JPAAddEntityTest.main(*
JPAAddEntityTest.java:38*)
Caused by: <openjpa-2.2.1-r422266:1396819 nonfatal store error> *
org.apache.openjpa.persistence.EntityExistsException*: An object of type
"com.ibm.wdp.bss.party.entity.PartyObject" with oid "0" already exists in
this context; another cannot be persisted.
at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(*
BrokerImpl.java:5080*)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(*
BrokerImpl.java:2653*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2573*)
at org.apache.openjpa.kernel.BrokerImpl.persist(*
BrokerImpl.java:2556*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(*
SingleFieldManager.java:800*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:621*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:589*)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(*
SingleFieldManager.java:505*)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(*
StateManagerImpl.java:3028*)
at org.apache.openjpa.kernel.PNewState.beforeFlush(*
PNewState.java:44*)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(*
StateManagerImpl.java:1042*)
at org.apache.openjpa.kernel.BrokerImpl.flush(*
BrokerImpl.java:2114*)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(*
BrokerImpl.java:2074*)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(*
BrokerImpl.java:1992*)
at org.apache.openjpa.kernel.LocalManagedRuntime.commit(*
LocalManagedRuntime.java:81*)
at org.apache.openjpa.kernel.BrokerImpl.commit(*
BrokerImpl.java:1516*)
at org.apache.openjpa.kernel.DelegatingBroker.commit(*
DelegatingBroker.java:933*)
at org.apache.openjpa.persistence.EntityManagerImpl.commit(*
EntityManagerImpl.java:570*)
... 1 more
The reason for this issue I found is, if the database which this object
being persisted, contains a row with primary key as 0, then adding new
record fails. Seems a duplicate primary key check is done when default
value (0) is assigned to the id attribute of type primitive long. A record
with Primary Key value = 0 already exists, so duplicate primary key check
fails.
This zip file contains a sample code with which I was able to reproduce
this error.
Adding a row with id=0 before persisting any object throws the error
mentioned above.
I request all to help in resolving this issue. The id 0 has been in use
for many days using hibernate. Hence we would like not to change this row
in the database.
Also would like to know if there is any config parameter to change the
default value of the id from 0 to any other value.
Thanks and Regards,
*KRISHNAPRASAD SUBBARAO*
Software Developer, Cloud Platform (BSS), Industry Solutions, India
Software Lab
--
Albert Lee.
Loading...