Parent relationships cannot be established after an object has been saved because the parent Key information is saved in the Child Key as a prefix. If an Artist is the parent of a Song, your Song key will looking something like this unencoded (this is just a pseudo-key):
Artist(Madonna):Song(Papa Don't Preach): If you save the Song object before the artist object, the Song already has the Key: Song(Papa Don't Preach) This cannot be changed. Rather than a parent-child relationship, you may be better off designing your objects to contain Keys of related objects. That is, your Song object should have a Key referencing the Artist. This'll allow you to save in arbitrary orders, though you may have to revisit your entity group setup if you need transactional writes. On Wed, Dec 23, 2009 at 2:15 PM, niraj <[email protected]> wrote: > I have three classes > > Album{ > Artist albumArtist; > @Persistent(mappedBy = "parent") > @Element(dependent = "true") > List<Song> songs; > } > > Song{ > Artist singer; > Album parent; > } > > Artist{ > String name; > } > > > If I use the Artist instance I create for Album for Song(singer) - I > get an error > > javax.jdo.JDOFatalUserException: Detected attempt to establish Album > (1) as the parent of Album(1)/Song(2)/Artist(3) but the entity > identified by Album(1)/Song(2)/Artist(3) is already a child of Album > (1)/Song(2). A parent cannot be established or changed once an object > has been persisted. > > CODE - THIS throws the above error > > Artist artist = new Artist("First", "LastN", "First Last"); > Album album = new Album("Ahat and album", artist, 1978); > Song song = new Song("Som song", album, artist); > album.addSong(song); > dao.makePersistantAndCommit(album); > > > CODE: This works > > Artist artist = new Artist("First", "LastN", "First Last"); > Artist artist2 = new Artist("Second", "LastN", "Second Last"); > Album album = new Album("Ahat and album", artist, 1978); > Song song = new Song("Som song", album, artist2); > album.addSong(song); > dao.makePersistantAndCommit(album); > > From my app requirements standpoint - I can have an Artist who is the > albumArtist and who is also the singer. And hence I have a need to use > the Artist instance. > > Inputs > > -- > > 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.
