Search code examples
javagoogle-app-enginepersistencejdo

Owned relationships in Google App Engine not working


I'm working on a Google App Engine project and I'm struggling to get relationships in JDO working properly. The objects get saved correctly however the related objects don't show up in the datastore viewer or in Queries.

A trimmed example of my code is:


        @PersistenceCapable
        public class Project implements Persistable {


    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent(defaultFetchGroup = "true")
    private Client client;

        // Extra fields and getter and setters remove

        }

        @PersistenceCapable
        public class Client implements Persistable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

Representation r = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {
            XMLToObject p = new XMLToObject(new org.everglade.tempus.persistence.dataobjects.Project());
            p.parse(xml);
            org.everglade.tempus.persistence.dataobjects.Project proj = (org.everglade.tempus.persistence.dataobjects.Project) p.getObject();
            pm.makePersistent(proj);
            getResponse().setStatus(Status.SUCCESS_CREATED);
            getResponse().setLocationRef("/project/" + KeyFactory.keyToString(proj.getId()));
        } catch (Exception e) { // Add finer exception handling here so can alter status based on what went wrong!
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
            Log.severe("The following error occurred whilst trying to add a new project: " + e.getMessage());
        } finally {
            pm.close();
        }

A project object is saved however no Client is associated with it - can anyone see why? This is my first ever app on app engine and also using JDO so it's probably something very obvious!

Thanks in advance!


Solution

  • You should know that owned relationships on App Engine are very different from the JDO spec. Owned relationships actually create an Entity Group which is a storage graph. More info here http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html

    So in most cases on app engine you are better off referencing other objects by Key. Unless you really meant to define an Entity Group graph. On the same note, I would advise you to avoid large inheritance trees (info in the same reference link above).