I have a Recipe. Each Recipe has an image. So my entity looks something like
@PersistenceCapable
public class Recipe {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private MyImage myImage;
When I create the Recipe the first time, it works great, the image is
added as well and I can view it. However when I go and update it such
as
PersistenceManager pm = PMF.get().getPersistenceManager();
Recipe r = pm.getObjectById(Recipe.class, recKey);
try {
r.setImage(newImage);
} finally {
pm.close();
}
the new image is added to the data-store, but when I try and fetch it
from within the recipe, the recipe still points to the old image in my
data-store. Is this normal? How can I fix this?
Here is the content of my jdoconfig.xml file
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
</persistence-manager-factory>
</jdoconfig>
I think the AppEngine implementation of JDO stores owned relationships in the form of parent keys. When you make myImageA a child of recipe1, appengine sets the parent of a MyImage entity to recipe1.
I’m not an expert on this, but I’m guessing that when you make myImageB a child of recipe1, appengine just sets the parent of another MyImage entity to recipe1. When it goes to retrieve
myImage, it looks for an image with a parent ofrecipe1, and still findsmyImageA, even thoughmyImageBis still sitting there.Again, I’m guessing. I wish there was a “Submit Guess” option.
TL;DR: I’d try deleting
myImageAexplicitly before settingmyImageB. That would break every other reference tomyImageA, but if you’re hoping to use it from other contexts an owned relationship is inappropriate anyway.This kind of confusing mixup is why I dropped JDO & owned relationships altogether and learned to love Objectify. They also constrain your options for entity groups, which adds another dimension of fog.