My guess is that you can't set childId_1 to just "child-1" but that it must
also include the parent's key, otherwise how does anybody know who the parent
is? My reading of their docs is that the keys include the parent. Look at the
section Creating and Using Keys, on http://xrl.in/3isq and the
KeyFactory.Builder example.
I'm with Andy; I don't see (understand) the need to set the id/key. Let
DataNucleus do that for you. You can retrieve it and store it elsewhere as a
foreign key if you need to.
Patrizio Munzi wrote:
> Hi again,
>
> I've just worked out that the problem is related to the gae.pk-name field.
> I though that the child encoded key string was set based on the pk-name
> field only if the encoded key field was null, instead it is set every
> time the object is written for the first time to the datastore,
> overriding the encoded key I set to put the second child in the same
> entity group of the parent.
> Hope I've been clear...!! :-)
>
> Shouldn't the encoded key string be left unchanged if already set???
> Is this bug??
> Have I missed something??
>
> Regards,
> Patrizio
>
> Patrizio Munzi wrote:
>> Hi all,
>>
>> I found another strange (unexpected behavior for me) with my two
>> simple Parent and Child classes and Entity Groups.
>> ASAIK in a transaction can be manipulated any object as long as they
>> belong to the same entity group.
>> Now, I cannot understand why I'm getting a Fatal exception when I try
>> to persist in a transaction an object for whom I set the encoded key
>> as child of the same parent.
>> Snippet in the following.
>>
>> Hope, Someone can help!
>>
>> Thanks
>>
>> Here's the snippet and my classes:
>>
>> ----------------- SNIPPET ------------------------------
>> public void testTransactions() {
>> String parentId = "parent";
>> String childId_1 = "child-1";
>> String childId_2 = "child-2";
>>
>> PersistenceManager pm = PMF.get().getPersistenceManager();
>> pm.currentTransaction().begin();
>> Parent parent = new Parent();
>> parent.setId(parentId);
>> Child child = new Child();
>> child.setId(childId_1);
>> child.setValue(100);
>> parent.setChild(child);
>> pm.makePersistent(parent);
>> pm.currentTransaction().commit();
>> pm.close();
>>
>> pm = PMF.get().getPersistenceManager();
>> pm.currentTransaction().begin();
>> String parentEncodedKey =
>> KeyFactory.createKeyString(Parent.class.getSimpleName(), parentId);
>> parent = pm.getObjectById(Parent.class, parentEncodedKey);
>> assertEquals(100, parent.getChild().getValue());
>> parent.getChild().setValue(1000);
>> child = new Child();
>> child.setId(childId_2);
>> Key child2Key =
>> KeyFactory.createKey(KeyFactory.stringToKey(parentEncodedKey),
>> Child.class.getSimpleName(), childId_2);
>> child.setEncodedKey(KeyFactory.keyToString(child2Key));
>> child.setValue(10);
>> pm.makePersistent(child);
>> pm.makePersistent(parent);
>> pm.currentTransaction().commit();
>> pm.close();
>>
>> pm = PMF.get().getPersistenceManager();
>> pm.currentTransaction().begin();
>> parentEncodedKey =
>> KeyFactory.createKeyString(Parent.class.getSimpleName(), parentId);
>> parent = pm.getObjectById(Parent.class, parentEncodedKey);
>> assertEquals(1000, parent.getChild().getValue());
>> pm.currentTransaction().commit();
>> pm.close();
>>
>> }
>> -----------------------------------------------
>> ---------------- PARENT -------------------------------
>> @PersistenceCapable(identityType = IdentityType.APPLICATION)
>> public class Parent implements Serializable {
>>
>> private static final long serialVersionUID = -3051163582728562957L;
>>
>> @PrimaryKey
>> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>> @Extension(vendorName="datanucleus", key="gae.encoded-pk",
>> value="true")
>> protected String encodedKey;
>>
>> @Persistent
>> @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
>> private String id;
>>
>> @Persistent(defaultFetchGroup="true")
>> @Element(dependent="true")
>> private Child child;
>>
>> public String getEncodedKey() {
>> return encodedKey;
>> }
>>
>> public void setEncodedKey(String encodedKey) {
>> this.encodedKey = encodedKey;
>> }
>>
>> public String getId() {
>> return id;
>> }
>>
>> public void setId(String id) {
>> this.id = id;
>> }
>>
>> public Child getChild() {
>> return child;
>> }
>>
>> public void setChild(Child child) {
>> this.child = child;
>> }
>>
>> }
>> -----------------------------------------------
>> ------------------- CHILD ----------------------------
>> @PersistenceCapable(identityType = IdentityType.APPLICATION)
>> public class Child implements Serializable {
>>
>> private static final long serialVersionUID = -3051163582728562957L;
>>
>> @PrimaryKey
>> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>> @Extension(vendorName="datanucleus", key="gae.encoded-pk",
>> value="true")
>> protected String encodedKey;
>>
>> @Persistent
>> @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
>> private String id;
>>
>> @Persistent
>> private long value;
>>
>> public String getEncodedKey() {
>> return encodedKey;
>> }
>>
>> public void setEncodedKey(String encodedKey) {
>> this.encodedKey = encodedKey;
>> }
>>
>> public String getId() {
>> return id;
>> }
>>
>> public void setId(String id) {
>> this.id = id;
>> }
>>
>> public long getValue() {
>> return value;
>> }
>>
>> public void setValue(long value) {
>> this.value = value;
>> }
>>
>> }
>> -----------------------------------------------
>>
>>
>>
>
> --
>
> *Patrizio Munzi*
> Product Specialist
> Viale Bruno Buozzi, 19 - 00197 Roma (Italy)
> tel: +39 06 4543 3540
> fax: +39 06 4543 3587
> mobile: +39 393 7195 164
> mail: [email protected] <mailto:[email protected]>
> web: http://www.eris4.com <http://www.eris4.com/>
> skype: eris4_munzi <skype:eris4_munzi?add>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---