Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » merge not updating pk IDs(JPA2)
merge not updating pk IDs [message #548449] Wed, 21 July 2010 21:23 Go to next message
Jay Hook is currently offline Jay HookFriend
Messages: 6
Registered: July 2010
Junior Member
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.
Re: merge not updating pk IDs [message #548589 is a reply to message #548449] Thu, 22 July 2010 12:14 Go to previous messageGo to next message
Arun Joy is currently offline Arun JoyFriend
Messages: 10
Registered: July 2010
Junior Member
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.
Re: merge not updating pk IDs [message #548615 is a reply to message #548589] Thu, 22 July 2010 13:34 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
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
Re: merge not updating pk IDs [message #548629 is a reply to message #548615] Thu, 22 July 2010 14:10 Go to previous messageGo to next message
Arun Joy is currently offline Arun JoyFriend
Messages: 10
Registered: July 2010
Junior Member
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)
Re: merge not updating pk IDs [message #548642 is a reply to message #548629] Thu, 22 July 2010 14:48 Go to previous messageGo to next message
Jay Hook is currently offline Jay HookFriend
Messages: 6
Registered: July 2010
Junior Member
I have nearly identical code to Arun and the exact same question.
Thanks for the quick response.
Jay
Re: merge not updating pk IDs [message #548990 is a reply to message #548629] Fri, 23 July 2010 15:38 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
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
Re: merge not updating pk IDs [message #549031 is a reply to message #548990] Fri, 23 July 2010 17:51 Go to previous messageGo to next message
Jay Hook is currently offline Jay HookFriend
Messages: 6
Registered: July 2010
Junior Member
Chris,

Thanks! Makes sense, that solved it. Really appreciate the quick response.
icon7.gif  Re: merge not updating pk IDs [message #549034 is a reply to message #548449] Fri, 23 July 2010 18:39 Go to previous messageGo to next message
Arun Joy is currently offline Arun JoyFriend
Messages: 10
Registered: July 2010
Junior Member
Chris, thanks a lot.
Best regards;
Re: merge not updating pk IDs [message #667535 is a reply to message #548449] Fri, 29 April 2011 12:21 Go to previous messageGo to next message
Matias  is currently offline Matias Friend
Messages: 2
Registered: April 2011
Junior Member
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.
Re: merge not updating pk IDs [message #1739318 is a reply to message #667535] Fri, 29 July 2016 14:53 Go to previous messageGo to next message
Guilherme Domingues is currently offline Guilherme DominguesFriend
Messages: 2
Registered: July 2016
Junior Member
Hello guys! I'm having the same problem decribed above, but no solution found. Can anyone help me, please?
Re: merge not updating pk IDs [message #1739329 is a reply to message #548990] Fri, 29 July 2016 18:37 Go to previous messageGo to next message
Guilherme Domingues is currently offline Guilherme DominguesFriend
Messages: 2
Registered: July 2016
Junior Member
Can you help me?
Re: merge not updating pk IDs [message #1739519 is a reply to message #1739329] Tue, 02 August 2016 15:51 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
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?
Previous Topic:SQL generated even for FetchType.LAZY reference
Next Topic:[Moxy Jersey] NLP at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:
Goto Forum:
  


Current Time: Thu Mar 28 13:54:23 GMT 2024

Powered by FUDForum. Page generated in 0.03448 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top