openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Krishnaprasad Subbarao <krishnapras...@in.ibm.com>
Subject Fw: Error in EntityManager.persist () if the database already contains a row with id=0
Date Tue, 19 Feb 2013 08:50:46 GMT
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@in.ibm.com
Chat: krishnaprasad.su@gmail.com 
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@openjpa.apache.org, 
Cc:     users <users@openjpa.apache.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@in.ibm.com
Chat: krishnaprasad.su@gmail.com 
Find me on:  and within IBM on:  


Tech Park One (panchshill)
Pune, MH 411006
India




From:   Rick Curtis <curtisr7@gmail.com>
To:     users <users@openjpa.apache.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 <
krishnaprasads@in.ibm.com> wrote:

> 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: com.ibm.wdp.bss.party.entity.PartyObject@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: com.ibm.wdp.bss.party.entity.PartyObject@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*


Mime
View raw message