I'm trying to model a polymorphic relationship in JDO, List<GameRound>
where each GameRound could be either a TextRound or PictureRound.

I've followed the documentation and modeled the association as a
List<Key> as described:
http://code.google.com/appengine/docs/java/datastore/relationships.html#Polymorphic_Relationships

// ... imports ...

@PersistenceCapable
public class Chef {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private Key favoriteRecipe;
}


Here is my Game object:

@PersistenceCapable
public class Game {

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

    @Persistent
        private List<Key> rounds = new ArrayList<Key>();
...
}


@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public class GameRound {
...
}


@PersistenceCapable
public class SentenceRound extends GameRound {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

...
}

@PersistenceCapable
public class PictureRound extends GameRound {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
...
}

The trouble is in my Game object I iterate through the rounds and try
to do a persistenceManager.getObjectById(GameRound.class, key) and I
get the following exception:

WARNING: /viewGames.jsp
javax.jdo.JDOFatalUserException: Exception converting SentenceRound(7)
to an internal key.
        at
org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManager.getObjectById(DatastoreJDOPersistenceManager.java:
68)
        at org.apache.jsp.viewGames_jsp._jspService(viewGames_jsp.java:121)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)


What's the proper way to retrieve a polymorphic key relationship (for
example how would you do it in the Appengine Chef example?).

My only other option is to model the relationship as mutually
exclusive properties of the GameRound (and manage that in my
application logic).

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public class GameRound {

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

        //Model relationships as optional components because JDO and
Appengine don't support
        //Polymorphic relationships well.

        @Persistent
        private SentenceRound sentenceRound;

        @Persistent
    private PictureRound pictureRound;

...
}

-- 
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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to