persistence@glassfish.java.net

Re: Persisting via cascading

From: christopher delahunt <christopher.delahunt_at_oracle.com>
Date: Mon, 23 Nov 2009 10:05:57 -0500

Can you Check if parent.getChildList().get(x) where x is the index of
the child you added has its Id set? Merge should return a managed
instance of your child object, so if you are using the reference to the
child you passed into merge, it will be different and may not have its
id set. The parent object returned from merge though should reference
the managed child which has its primary key set.

Best Regards
Chris

Gordon Yorke wrote:
> This should work. Is the child's ID generated? What Generator Type
> is used? Is the ID a primitive value or an Object wrapper?
> --Gordon
>
> Gary Jacobson wrote:
>> I recently attempted to switch from Toplink Essentials to Eclipselink
>> and encountered a problem. I have some code which looks like this:
>>
>> // create new child entity
>> Child child = new Child();
>>
>> // add it to the parent entity, which has @OneToMany( cascade =
>> CascadeType.ALL ) on its childList
>> child.setParent( parent );
>> parent.getChildList().add( child );
>>
>> // now I hand off to a stateless session bean which merges the parent
>> using em.merge()
>> parent = myFacade.merge( parent );
>>
>> // retrieve child id
>> log( child.getId() );
>>
>> At this point, using Toplink, I could get the new child's id.
>> However, using Eclipselink, it returns 0, despite the fact that the
>> child was successfully created and added to the parent. Is this a
>> bug, or is there some sort of best practice I'm not using? I'm hoping
>> to avoid using any Eclipselink-specific code.
>>
>> Thanks
>> Gary
>>