views:

132

answers:

4

Hi

Have a class:

class Node implements Serializable
{
    private String name;

    public String getName { return name; }
    public void setName(String val){ name = val; }

    public Node(){}
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
class NodeBag implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent(serialized="true")
    private ArrayList<Node> nodes = new ArrayList<Node>();

    public String getNodes { return nodes; }
    public void setNodes(ArrayList<Node> val){ nodes = val; }

    public NodeBag(){}
}

I can save it to the db with this

PersistenceManager pm = PMF.getManager();
try
{
 pm.makePersistent(newBag);
}
finally
{
 pm.close();
}

But when i load it back

PersistenceManager pm = PMF.getManager();
Query q = pm.newQuery(NodeBag.class);
try
{
 List<NodeBag> pipelines = (List<NodeBag>)q.execute();
 return nodeBags; // nodeBags[0].nodes is always empty
}
finally
{
 q.closeAll();
}

Nodebag.nodes is always empty!

Did i miss something?

Thanks in advance.

Regards, Paul

A: 

Missed putting it in the fetch plan ? mark in default fetch group perhaps, or access the field, or put in a custom fetch plan, as per the DataNucleus docs and JDO spec.

DataNucleus
A: 

Actually, i wanted also the return the answer across the wire by converting it to JSON. And i've managed to load the child objects. The trick i use is detach. By detaching, everything will be loaded.

Thanks.

Colossal Paul
A: 

In your call to return the objects you can use the FetchPlan to specify what FetchGroup to return. See JDO docs for more information on the FetchGroup options.

You can ensure that all the entities are fetched, by specifying in your PersistenceManager the FetchGroup to use. The modified code is shown below:

PersistenceManager pm = PMF.getManager();
pm.getFetchPlan().setGroup(FetchGroup.ALL);
Query q = pm.newQuery(NodeBag.class);
try {
  List<NodeBag> pipelines = (List<NodeBag>)q.execute();
  return nodeBags; // nodeBags[0].nodes is always empty
} finally {
  q.closeAll();
}
bennettweb
A: 

I had a heck of a time getting fetch groups to work. Both Query and PersistenceManager have a getFetchPlan(), but only the one on PersistenceManager seems to work.

Also, make sure you make your objects detachable and use pm.detachCopyAll() on the result.

Mike Miller