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 Hook
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 Joy
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 Delahunt
Messages: 1023
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 Joy
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 Hook
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 Delahunt
Messages: 1023
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 Hook
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 Joy
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 message
Matias  is currently offline Matias
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.
Previous Topic:Apparent interface violation in CriteriaBuilderImpl
Next Topic:Relationships/FKs to non-PK columns
Goto Forum:
  


Current Time: Tue Sep 23 08:28:13 GMT 2014

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

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