Hi Jason and Max,

I was making some tests on this today and I've found out that one-to-many relationship child objects retrieving works as long as the one-to-many relationship doesn't involve polymorphisms.
For example, if we make the Child class abstract and save subclasses into the list the retrieving fails.

In the following the snippet that fails.
Is this a bug?
Am I doing something wrong??

Thanks

-----------------------
        String parentId = "test";
        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.currentTransaction().begin();
        Parent parent = new Parent();
        Child subChild = new SubChild();
        subChild.setParentId(parentId);
        parent.getChilds().add(subChild);
        pm.makePersistent(parent);
        pm.currentTransaction().commit();
        pm.close();

        pm = PMF.get().getPersistenceManager();
        parent = pm.getObjectById(Parent.class, parentId);
        assertEquals(1, parent.getModules().size());
        pm.close();
-----------------------
-----------------------
public abstract class Child implements Serializable {

    private static final long serialVersionUID = -5125563546796512541L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    protected String encodedKey;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
    protected String parentId;

    [...]
}

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

    private static final long serialVersionUID = 521265701642620882L;

    @Persistent(defaultFetchGroup="true")
    private String name;

}
----------------------

Jason (Google) wrote:
Max is right, although there's a small issue with the syntax:

@Persistent(mappedBy="parent", defaultFetchGroup = "true")
public List<Child> childs;

- Jason

On Mon, Oct 19, 2009 at 10:26 PM, Max Zhu <thebb...@gmail.com> wrote:
Hi Lars,

Try to annotate your relationship as follows:

       @Persistent(mappedBy="parent", default-fetch-group="true")
       public List<Child> childs;


On Tue, Oct 20, 2009 at 12:19 AM, Lars <lsor...@gmail.com> wrote:

Hi,
I am failing to retrieve child-objects in a one-to-many JDO relation
in the datastore. The case is as follows;

I have two classes (Parent & Child, code-snippet below) with a defined
one-to-many relation.
It is no problem storing the structure with the 'store'-operation
defined below. This is easily verified by web-browsing the datastore.

However, when retrievning the parent-object from the datastore
('fetchParents'), the ''childs' attribute is always null. What must be
done to (auto-)populate this attribute from the datastore?
Also, the 'parent'-attribute of the Child-objects will also be null if
they are fetched in a similar way.

All clues appreciated...

Lars

- - - - - - - Code samples below - - - - - -

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ParentDTO  {
       @PrimaryKey
       @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   @Extension(vendorName="datanucleus", key="gae.encoded-pk",
value="true")
       private String encodedKey;

       @Persistent
       public String name;

       @Persistent(mappedBy="parent")
       public List<Child> childs;

       public ParentDTO()
       {

       }

       public void add(Child c)
       {
               if (childs == null)
                       childs = new ArrayList<Child>();
               kids.add(c);
       }
}

 - - - -

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Child {
       @PrimaryKey
       @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   @Extension(vendorName="datanucleus", key="gae.encoded-pk",
value="true")
       public String encodedKey;
       @Persistent
       public String name;

       @Persistent
       public Parent parent;

       public Child()
       {
       }

       public String getEncodedKey() {
               return encodedKey;
       }
}

- - - - -

Storing to datastore (works perfectly)

               public void store()
               {
                       Parent p = new Parent();
                       p.navn = "nils";

                       Child c = new Child();
                       c.name = "jim";
                       p.add(c);

                       c = new ChildDTO();
                       c.name = "anne";
                       p.add(c);

                       PersistenceManager pm = PMF.get().getPersistenceManager();
                       try {
                               pm.makePersistent(p);
                       } catch (Exception ee) {
                               res = ee.getMessage();
                       } finally {
                               pm.close();
                       }
               }

 - - - - - - Fetching data (not working)

               public String fetchParents()
               {
                   String res = "";
                   PersistenceManager pm = PMF.get().getPersistenceManager();

                   javax.jdo.Query query = pm.newQuery(Parent.class);
                   List<Parent> parents = (List<Parent>) query.execute();
                   Iterator<Parent> iterF = parents.iterator();
                       while (iterF.hasNext()) {
                               Parent f = iterF.next();
                               res = res + ":" + f.name;
                               if (f.childs != null) { // this is the problem - 'this.childs' is
always null
                                       Iterator<Child> iterI = f.childs.iterator();
                                       while (iterI.hasNext()) {
                                               Child idto = iterI.next();
                                               res = res + ">" + idto.name;
                                       }
                               }
                       }
                   pm.close();
               }









--

Patrizio Munzi
Product Specialist
Viale Bruno Buozzi, 19 - 00197 Roma (Italy)
tel: +39 06 4543 3540
fax: +39 06 4543 3587
mobile: +39 393 7195 164
mail: patrizio.mu...@eris4.com
web: http://www.eris4.com
skype: eris4_munzi


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