Hi Doug, You should do some key.toString() on your keys to understand your problem: the Key contains the name of the class by default. So, when you query on GameRound, you'll never find a SentenceRound or a PictureRound.
[Also check http://code.google.com/appengine/docs/java/datastore/relationships.html#Polymorphic_Relationships] You have to use the KeyFactory class to generate the proper key to allow for your polymorphic relation ship. See available examples at http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Creating_and_Using_Keys. Secondly, after they keys are right, you have to make sure that the DataStore stores your different entities in the same table via the annotation @Inheritance(custom-strategy=xxx). See http://www.jpox.org/docs/1_2/jdo_orm/inheritance.html. regards didier On Jul 12, 3:05 am, Doug Daniels <daniels.doug...@gmail.com> wrote: > 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.ht... > > // ... 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.