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.
