RE: [jira] Created: (OPENJPA-245) Attach NEW and auto-increment identity
The entity is using auto-generated identity - please see the attached orm.xml. I am creating NEW instance and assigning an already existing primary key only to reproduce the problem. The real-life scenario is that a web service performs de-serialization of input XML parameter and, in the process, creates a new instance of an entity that has a primary key that already exists in database (this is done by server-side code generated using WL jwsc ant task from WL 9.2.1). em.merge() apparently doesn't work correctly in this case, since a new record is created in database on commit. Regards, Aleksandar Likic Pinaki Poddar wrote: > >>> just create a new instance of an entity, assign an already existing > primary key >>> A new record will be created in database, with new, auto-generated > primary key. > > What is the identity mechanicsm used by the entity class? > If the entity is using auto-generated identity then assigning an > "already existing" key is not recommneded. > If the entity is not using auto-generated, then assigning an existing > key *should* fail at commit with duplicate key exception. > > > Pinaki Poddar > BEA Systems > 415.402.7317 > > > -Original Message- > From: Aleksandar Likic (JIRA) [mailto:[EMAIL PROTECTED] > Sent: Sunday, May 27, 2007 9:05 AM > To: open-jpa-dev@incubator.apache.org > Subject: [jira] Created: (OPENJPA-245) Attach NEW and auto-increment > identity > > Attach NEW and auto-increment identity > -- > > Key: OPENJPA-245 > URL: https://issues.apache.org/jira/browse/OPENJPA-245 > Project: OpenJPA > Issue Type: Bug > Components: jpa > Affects Versions: 0.9.7, 0.9.6 > Environment: jdk1.5.0_11, Win XP, Fedora Core 6, Postgres 8.1 > (on Fedora) > Reporter: Aleksandar Likic > > > According to documentation (1.2 Attach Behavior), when an entity > instance is NEW (never detached): > > * If neither of the above cases apply, OpenJPA will check to see if > an instance with the same primary key values exists in the database. If > so, the object is considered detached. Otherwise, it is considered new. > > This doesn't work for me - a new record in database is created on commit > instead of updating the existing one. The "regular" case - > detach/modify/attach works fine - the existing record is updated. > > It is very easy to reproduce - just create a new instance of an entity, > assign an already existing primary key, call em.merge() and commit. A > new record will be created in database, with new, auto-generated primary > key. > > I stumbled on this trying to implement a web service that uses > OpenJPA-based backend. When servicing an "update" request, the web > service instantiates a NEW object (by performing XML de-serialization) > and calls em.merge to update the entity. A new record gets created > instead of updating an existing one. > > Entity class (START) -- > > package exaple; > > public class Consumer implements java.io.Serializable { > > private long id; > > public long getId() { > return this.id; > } > > public void setId(long id) { > this.id = id; > } > > private java.lang.String firstName; > > public java.lang.String getFirstName() { > return this.firstName; > } > > public void setFirstName(java.lang.String firstName) { > this.firstName = firstName; > } > > private java.lang.String lastName; > > public java.lang.String getLastName() { > return this.lastName; > } > > public void setLastName(java.lang.String lastName) { > this.lastName = lastName; > } > > Entity class (END) -- > persistence.xml (START) -- > xmlns="http://java.sun.com/xml/ns/persistence"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; version="1.0"> > > > > > org.apache.openjpa.persistence.PersistenceProviderImpl er> > > > example.Consumer > > > > value="postgres"/> > value="org.postgresql.Driver"/> > value="app_user"/> > value="app_user"/> > value="jdbc:postgresql://localhost/alikic"/> > value="DefaultLevel=WARN,SQL=TRACE"/> > > > > > > persistence.xml (END) -- > orm.xml (START) -- > http://java.sun.com/xml/ns/persistence/orm"; > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm > orm_1_0.xsd" > version="1.0"> > > > > > > > > > > > > > > > --
RE: [jira] Created: (OPENJPA-245) Attach NEW and auto-increment identity
>> just create a new instance of an entity, assign an already existing primary key >> A new record will be created in database, with new, auto-generated primary key. What is the identity mechanicsm used by the entity class? If the entity is using auto-generated identity then assigning an "already existing" key is not recommneded. If the entity is not using auto-generated, then assigning an existing key *should* fail at commit with duplicate key exception. Pinaki Poddar BEA Systems 415.402.7317 -Original Message- From: Aleksandar Likic (JIRA) [mailto:[EMAIL PROTECTED] Sent: Sunday, May 27, 2007 9:05 AM To: open-jpa-dev@incubator.apache.org Subject: [jira] Created: (OPENJPA-245) Attach NEW and auto-increment identity Attach NEW and auto-increment identity -- Key: OPENJPA-245 URL: https://issues.apache.org/jira/browse/OPENJPA-245 Project: OpenJPA Issue Type: Bug Components: jpa Affects Versions: 0.9.7, 0.9.6 Environment: jdk1.5.0_11, Win XP, Fedora Core 6, Postgres 8.1 (on Fedora) Reporter: Aleksandar Likic According to documentation (1.2 Attach Behavior), when an entity instance is NEW (never detached): * If neither of the above cases apply, OpenJPA will check to see if an instance with the same primary key values exists in the database. If so, the object is considered detached. Otherwise, it is considered new. This doesn't work for me - a new record in database is created on commit instead of updating the existing one. The "regular" case - detach/modify/attach works fine - the existing record is updated. It is very easy to reproduce - just create a new instance of an entity, assign an already existing primary key, call em.merge() and commit. A new record will be created in database, with new, auto-generated primary key. I stumbled on this trying to implement a web service that uses OpenJPA-based backend. When servicing an "update" request, the web service instantiates a NEW object (by performing XML de-serialization) and calls em.merge to update the entity. A new record gets created instead of updating an existing one. Entity class (START) -- package exaple; public class Consumer implements java.io.Serializable { private long id; public long getId() { return this.id; } public void setId(long id) { this.id = id; } private java.lang.String firstName; public java.lang.String getFirstName() { return this.firstName; } public void setFirstName(java.lang.String firstName) { this.firstName = firstName; } private java.lang.String lastName; public java.lang.String getLastName() { return this.lastName; } public void setLastName(java.lang.String lastName) { this.lastName = lastName; } Entity class (END) -- persistence.xml (START) -- http://java.sun.com/xml/ns/persistence"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; version="1.0"> org.apache.openjpa.persistence.PersistenceProviderImpl example.Consumer persistence.xml (END) -- orm.xml (START) -- http://java.sun.com/xml/ns/persistence/orm"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" version="1.0"> orm.xml (END) -- -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. Notice: This email message, together with any attachments, may contain information of BEA Systems, Inc., its subsidiaries and affiliated entities, that may be confidential, proprietary, copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.