Thanks for the reply, Erick.

I'm trying persist data using JDO (a one-to-many relationship).

I did a code sample that inserted the children, but the parents isn't
linked to those children.

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable = "true")
public class Book {

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

    private String title;

    @Persistent(mappedBy = "book")
    @Element(dependent = "true")
    private List<Chapter> chapters = new ArrayList<Chapter>();

    // getters and setters
}


import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.IdentityType;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable = "true")
public class Chapter {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key id;

        private String title;
        private int numPages;

        @Persistent
        private Book book;

        // getters and setters
}

                Book b = new Book();
                b.setTitle("JDO 4eva");
                Chapter c1 = new Chapter();
                c1.setTitle("Intro");
                c1.setNumPages(10);
                b.getChapters().add(c1);
                Chapter c2 = new Chapter();
                c2.setTitle("Configuration");
                c2.setNumPages(9);
                b.getChapters().add(c2);

                pm.currentTransaction().begin();
                try {
                    pm.makePersistent(b);
                    pm.currentTransaction().commit();
                } finally {
                    if (pm.currentTransaction().isActive()) {
                        pm.currentTransaction().rollback();
                    }
                }

The book and its chapters are inserted, but when I retrieve that book,
its Chapters List is empty.

-- 
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.

Reply via email to