You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Ryan Cornia <rc...@myriad.com> on 2008/01/11 23:42:34 UTC

Merge with changes to a primary key?

I have a legacy database that has a compound primary key. For example,
Report with a  primary key type ReportId. (which is composed of a long and a
couple of strings.)

If I create a new ReportId, and assign it to a new Report, then call
persist, the entity is saved correctly.

If, on the other hand, I load an existing report, and change one of the
fields on the reportId, the change is not saved when calling flush.

Help! This has me stumped. The updates work on other objects that are not a
primary key field, but it does not work with a compound primary key change.
I am not sure how to code it to save changes to a compound primary key.

Thanks,
Ryan



Re: Merge with changes to a primary key?

Posted by Pinaki Poddar <pp...@apache.org>.
You are effectively changing the primary key of an instance. The 'persistent
identity' of the instance has changed, but 'Java reference' to the instance
has not changed. OpenJPA (or for that matter any other JPA provider, I
guess) does not handle this scenario.  

One way to treat this scenario will be: 
a) the original entity is deleted
b) a new entity is created with the changed persistent identity 
c) all relations to this new entity is adjusted to the same as the original
entity. 

An example is attached to demonstrate how this can be done.
The example uses a Person and Address in a bi-directional 1:1 relation.
Person uses 'name' field as its primary key. Simply changing a Person's name
in setName() method is not going to change the database record. But if
changing name creates a cloned Person with a changed name and address
pointing to the address of the original Person (see Person.changeName()
method), then the a new database record for Person with changed name is
inserted. 

What happens to the Person record with the original name? Depends on the
application semantics. In this simple test case, it is simply deleted.  

http://www.nabble.com/file/p14789634/TestChangePK.java TestChangePK.java 
http://www.nabble.com/file/p14789634/Address.java Address.java 
http://www.nabble.com/file/p14789634/Person.java Person.java 
-- 
View this message in context: http://www.nabble.com/Merge-with-changes-to-a-primary-key--tp14766949p14789634.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.