JDO 未获取集合成员字段
有一个类:
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(){}
}
我可以用这个将它保存到数据库
PersistenceManager pm = PMF.getManager();
try
{
pm.makePersistent(newBag);
}
finally
{
pm.close();
}
但是当我加载它时
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 总是空的!
我错过了什么吗?
提前致谢。
问候, 保罗
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在返回对象的调用中,您可以使用 FetchPlan 来指定要返回的 FetchGroup。有关 FetchGroup 选项的更多信息,请参阅 JDO 文档。
您可以通过在 PersistenceManager 中指定要使用的 FetchGroup 来确保提取所有实体。修改后的代码如下所示:
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:
我在让提取组工作时遇到了很大的困难。
Query
和PersistenceManager
都有一个getFetchPlan()
,但只有PersistenceManager
上的那个似乎可以工作。另外,请确保您的对象可分离,并对结果使用
pm.detachCopyAll()
。I had a heck of a time getting fetch groups to work. Both
Query
andPersistenceManager
have agetFetchPlan()
, but only the one onPersistenceManager
seems to work.Also, make sure you make your objects detachable and use
pm.detachCopyAll()
on the result.错过了将其放入获取计划中吗?根据 DataNucleus 文档和 JDO 规范,可能会在默认获取组中进行标记,或者访问该字段,或者放入自定义获取计划中。
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.
实际上,我还想通过将答案转换为 JSON 来返回答案。
我已经成功加载了子对象。我使用的技巧是分离。通过分离,所有内容都将被加载。
谢谢。
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.
使用 Collection 而不是 List,只要它位于事务内,它就应该开始工作
Use Collection insted of List and it should start working as long as it is inside a transaction