I think I have found a solution to the problem.
Note: in the solution, I have assumed that there is no way I could
retrieve the key after persisting the child entity without an
additional query.
In order to achieve what I need, the key of the child entity has to be
manually created with the parent entity key info. See the following
lines:
Key childPk = new KeyFactory.Builder(ParentEntity.class.getSimpleName(),
parentDao
.getPk().getName()).addChild(ChildEntity.class.getSimpleName(),
childDao.getPk().getName()).getKey();
Both the child and parent keys have to be generated first with the
KeyFactory. In this case, I have the following:
parentPk = ParentEntity("3f9fc099-d090-4868-ae69-456e25ab4a57")
childPK = ChildEntity("30c6ea1f-ab88-4362-9e39-b52b2f5a0176");
After using the Builder, the childPK becomes:
ParentEntity("3f9fc099-d090-4868-ae69-456e25ab4a57")/
ChildEntity("30c6ea1f-ab88-4362-9e39-b52b2f5a0176");
Please correct me if I am wrong, thanks.
On 1 Jan 2010, at 16:18, Mike wrote:
> Hi,
>
> I have two entities - Parent and Child, the relationship is "mapped"
> using the mappedBy annotations.
>
> Both entities are using Key type as the pk, which is manually
> generated during the instantiation of the object.
>
> When the PK is generated for the Child, parent information in the
> child key is omitted because both parent and child are not yet
> persisted. However, after persisting the parent and then the child.
> The child's key changed because parent key information is appended to
> the child's key, and currently I could not find a way to retrieve the
> updated key without additional queries after pm.makePersistent
> (childEntity) is called.
>
> Now this is my question. How do I retrieve the updated Key? I have
> tried pm.detachCopy(childEntity) with no result. Could someone please
> give me some advice? Do I need to manually append parent key with
> KeyFactory.Builder? If so, I do I achieve it with the Key type?
>
> Thanks in advance
>
> Michael
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "Google App Engine for Java" group.
> To post to this group, send email to [email protected]
> .
> To unsubscribe from this group, send email to
> [email protected]
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en
> .
>
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.