merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/548449/#msg_548449
When I merge a parent entity that already been persisted, but has newly created children objects, the new PK for the child objects do not get populated into the java object. The store works fine, but the children PK are still null. Am I missing something configuration wise? I can post code samples if helpful.

I Saved A. Then Added an Item to list Bs. Then merged A. The new B is saved in the database, but the id of new B is still 0. so when i try to merge A again, B gets overwritten.]]>Arun Joy2010-07-22T12:14:32-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/548615/#msg_548615
Can you give an example showing how you are saving/merging and checking the B objects? When you call merge it will return the managed version of the entity passed in. This means that new objects passed into merge remain unmanaged and will not have IDs set. This is different from em.persist, where the entity passed in becomes managed, and so can continue to be used and will have its ID set.

If the A entity being used after it is 'saved' is managed, there is no need to merge it. If it is detached, then you will need to be sure you return the managed A object after the merge, as only it will reference the managed B objects with the IDs set which can then be used in the next merge.

So if i have a particular scenario like I need to add the parent first. then Add the children later (when parent is not managed). How do I save the new children and relation (without calling save on each child and then a merge on parent--- it is time consuming)]]>Arun Joy2010-07-22T14:10:34-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/548642/#msg_548642
Thanks for the quick response.
Jay]]>Jay Hook2010-07-22T14:48:29-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/548990/#msg_548990
I assume the first persist is wrapped in a transaction that commits. That means you need to call merge as A will be detached - what em.merge returns is the managed A and B. ie, assuming,
A managedA = em.merge(detachedA);
then managedA != detachedA, they are different instances. So the JPA provider will set the primary key in entities referenced frommanagedA when it can (either right away, at flush or when the transaction commits depending on the sequencing options and the provider implementation), but your application will never see any changes in the detachedA.

You need to pass back the A object returned from em.merge(A) to the application. If the primary key is still not set, you may need to call flush before returning the A.

Best Regards,
Chris]]>Chris Delahunt2010-07-23T15:38:58-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/549031/#msg_549031
Thanks! Makes sense, that solved it. Really appreciate the quick response. ]]>Jay Hook2010-07-23T17:51:56-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/549034/#msg_549034
Best regards;]]>Arun Joy2010-07-23T18:39:53-00:00Re: merge not updating pk IDshttps://www.eclipse.org/forums/index.php/mv/msg/172394/667535/#msg_667535
I have a problem, we are migrating many applications built with JPA and TopLink Essentials to EclipseLink , in the case of TopLink Essentials operated as follows:

objectA = em.merge (objectB)
objectA == objectB

Then in objectB was set the pk of the entity. But eclipselink as explained in previous messages do not work this way and the pk of objectB remains null. There is any way to get the behavior described above, since the impact of changing the whole implementation would be too much.