I'm a little new to eclipselink, but I'm having an issue I hope someone can help me with.
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.
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.
public class myLogic
public void executeLogic()
ejb.merge(A); //When i check DB I can find an entry A->B
System.out.println(A.Bs.getIndex(0).getId()); // returns 0
public class ejb
@PersistenceContext(unitName = "PUName")
private EntityManager em;
public void saveEntity(A a)
public void mergeEntity(A a)
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)
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.
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.
Guilherme Domingues -As described, merge returns the managed instance - this is a copy of what you passed in. So when the transaction completes or is flushed, the managed instance should have its primary set while the copy you passed into merge will not. what problem exactly are you having and what have you tried?