2

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

5 Answers 5

1

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();
}
1

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.

0
0

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.

0

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.

0

Use Collection insted of List and it should start working as long as it is inside a transaction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.