使用 A 的实例列表持久化 A 类
看一下下面的代码示例。
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class A implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String key;
@Persistent
private String m_Name;
@Persistent(mappedBy = "m_A")
private List<B> m_ListOfB;
//...
}
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable="true")
public class B implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String key;
@Persistent
private A m_A;
@Persistent
private List<B> m_Children;
//...
}
当我设置 A 的实例,并在 m_ListOfB 列表中包含一些 B(这些 B 实例也有一些子级)并保留它时,我在数据存储查看器中只看到 A。 如果我从 B 中删除 m_Children,并尝试再次保留 A,我会看到 A 和 m_ListOfB 中的对象。
像这样持久化
A a = new A("a");
B b = new B("b");
b.add_to_children(new B("child_of_b"));
a.add_to_list(b);
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(a);
pm.close();
列表在构造函数中正确设置,并且我有默认的空 Ctor 以使可序列化不会失败。
也许这是索引的问题? datastore-indexes.xml 看起来像这样
<datastore-index kind="B" ancestor="true" source="manual">
<property name="m_ListOfB_INTEGER_IDX" direction="asc"/>
</datastore-index>
不确定 m_Children 的索引应该是什么样子,或者我是否需要添加它。
有什么想法吗? 谢谢
Take a look at the following code sample.
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class A implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String key;
@Persistent
private String m_Name;
@Persistent(mappedBy = "m_A")
private List<B> m_ListOfB;
//...
}
@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable="true")
public class B implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String key;
@Persistent
private A m_A;
@Persistent
private List<B> m_Children;
//...
}
When i set up a instance of A, with a few B's in the m_ListOfB-list (These B-instanced have a few Children aswell) and persist it, i only see A in my datastore viewer.
If i remove the m_Children from B, and try to persist A again, i see A and the objects in m_ListOfB's.
Persisted like this
A a = new A("a");
B b = new B("b");
b.add_to_children(new B("child_of_b"));
a.add_to_list(b);
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(a);
pm.close();
Lists are set up correctly in the constructors, and i have the default empty Ctor for Serializable not to fail.
Perhaps this is an issue of indexes?
The datastore-indexes.xml looks like this
<datastore-index kind="B" ancestor="true" source="manual">
<property name="m_ListOfB_INTEGER_IDX" direction="asc"/>
</datastore-index>
Not sure how the index for m_Children should look, or if i even need to add it.
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论