If you use the loop to continue to addChild() on the Key, your Key will look something like this:
Page1->IMG_A->IMG_B->IMG_C What you want are Keys that look like this: Page1->IMG_A Page2->IMG_B If you continue to set children on the same builder object, you will likely be setting Images to be Parents of other Images. Unless you need human readable Keys for the IMG class, you can just set them as children of the Parent and persist the Parent. An alternative implementation for one-to-many relationships is to store a List or Set of IMG Keys on the Page and a persistable Page Key on the IMG object. The Keys will not store ancestor information, so it could end up easier to manage. On Wed, Jan 13, 2010 at 7:39 AM, Ftaylor <[email protected]>wrote: > I have a class Page with a variable List<IMG> images. > > @PersistenceCapable(identityType = IdentityType.APPLICATION) > public class Page { > @PrimaryKey > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) > private Key key; > > @Persistent > private final List<IMG> images; > > ... > > } > > My IMG class looks like this: > > @PersistenceCapable(identityType = IdentityType.APPLICATION) > public class IMG { > > ... > > @PrimaryKey > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) > private final int number; > > ... > > } > > I don't fully understand how the one to many key using > KeyFactory.Builder works. In order to make an instance of Page > persistent using the datastore, do I need to loop through every IMG > instance like this: > > for(IMG image : images) { > KeyFactory.Builder keyBuilder = new KeyFactory.Builder > (Page.class.getSimpleName(), "page-id"); > keyBuilder.addChild(IMG.class.getSimpleName(), image.getID()); > Key key = keyBuilder.getKey(); > image.setKey(key); > pm.makePersistent(image); > } > > or can I do this? > > KeyFactory.Builder keyBuilder = new KeyFactory.Builder > (Page.class.getSimpleName(), "page-id"); > for(IMG image : images) { > keyBuilder.addChild(IMG.class.getSimpleName(), image.getID()); > } > Key key = keyBuilder.getKey(); > for(IMG image : images) { > image.setKey(key); > pm.makePersistent(image); > } > > or can I just do this? > > Page p; > ... > Key key = KeyFactory.createKey(Page.class.getSimpleName(), page.getID > ()); > pm.makePersistent(page); > > -- > 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]<google-appengine-java%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-appengine-java?hl=en. > > > > -- Ikai Lan Developer Programs Engineer, Google App Engine--
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.
