text: Eclipse Community Forums - RDF feed // Eclipse Community Forums merge not updating pk IDs // <![CDATA[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. Thanks. ]]> Jay Hook 2010-07-21T21:23:32-00:00 Re: merge not updating pk IDs // <![CDATA[I am getting a similar issue. class A { @OneToMany(cascade = CascadeType.ALL) List<B> Bs; } B { @Id @GeneratedValue(strategy = GenerationType.AUTO) long id; } Scenario : 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 Joy 2010-07-22T12:14:32-00:00 Re: merge not updating pk IDs // <![CDATA[Hello, 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. Best Regards, Chris]]> Chris Delahunt 2010-07-22T13:34:19-00:00 Re: merge not updating pk IDs // <![CDATA[public class myLogic { public void executeLogic() { ejb.save(A); Bs.add(new B); ejb.merge(A); //When i check DB I can find an entry A->B System.out.println(A.Bs.getIndex(0).getId()); // returns 0 } } eg: @Stateless public class ejb { @PersistenceContext(unitName = "PUName") private EntityManager em; public void saveEntity(A a) { em.persist(A); } public void mergeEntity(A a) { em.merger(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)]]> Arun Joy 2010-07-22T14:10:34-00:00 Re: merge not updating pk IDs // <![CDATA[I have nearly identical code to Arun and the exact same question. Thanks for the quick response. Jay]]> Jay Hook 2010-07-22T14:48:29-00:00 Re: merge not updating pk IDs // <![CDATA[Hello, 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 Delahunt 2010-07-23T15:38:58-00:00 Re: merge not updating pk IDs // <![CDATA[Chris, Thanks! Makes sense, that solved it. Really appreciate the quick response. ]]> Jay Hook 2010-07-23T17:51:56-00:00 Re: merge not updating pk IDs // <![CDATA[Chris, thanks a lot. Best regards;]]> Arun Joy 2010-07-23T18:39:53-00:00 Re: merge not updating pk IDs // <![CDATA[Hi, 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. Thanks in advance for your response. ]]> Matias 2011-04-29T12:21:44-00:00