Hello, I need to know if it's possible to add childs after the parent
is save in the datastore
See the code after : Parent class, Child class and code to create a
new Parent and Child.

/* Parent entity */
@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable = "true")
public class Parent{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key idParent;
    @Persistent
        private String Name;
    @Persistent(mappedBy = "p")
    private List<Child> children= new ArrayList<Child>();

    public Parent(String Name) { this.Name=Name; }

    //getters and setters
    public Key getIdParent() { return idParent; }
    public String getName() { return Name; }
    public void setName(String Name) { this.Name= Name; }
    public List<Child> getChild() { return children; }
    public void setChild(List<Child> children) { this.children=
children; }
    public void addChild(Child c){
        ArrayList<Child> k = new ArrayList<Child>(children);
        k.add(c);
        setChild(k);
    }

 /*Child entity*/
@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable = "true")
public class Child{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key idChild;
    @Persistent
        private int Num;
    @Persistent
        private Parent p;

        public Child(int Num) {this.Num = Num;}

        //getters and setters
        public Key getIdChild() { return idChild; }
        public void setIdChild(Key idChild) { this.idChild= idChild; }
        public int getNum() { return Num; }
        public void setNum(int Num) { this.Num = Num; }
        public Parent getParent() { return p; }
        public void setParent(Parent p){ this.p = p; }

/*code to create one parent and add a child to it*/
//new parent
Parent p = new Parent("parent");
PersistenceManager pm = PMF.get().getPersistenceManager();
 try {
         pm.makePersistent(p);
 } finally {
         pm.close();
 }
//new child
Child c = new Child(1);
c.setParent(p);
p.addChild(c);
pm = PMF.get().getPersistenceManager();
 try {
         pm.makePersistent(c);
 } finally {
         pm.close();
 }

This code gives an error of the server but the parent is saved in the
datastore.
Thank's for reading and sorry for my english.

-- 
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-java@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