AppEngine:以拥有的、双向的一对多关系保留孤儿
我有以下两个实体:
@Entity
public class SupermarketChain {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String key;
@OneToMany(mappedBy = "supermarketChain")
@Basic
private List<Supermarket> supermarkets;
}
@Entity
public class Supermarket {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String key;
@ManyToOne(optional=true)
private SupermarketChain supermarketChain;
}
当我使用 em.remove(SupermarketChain.class, key) 删除父实体时,所有孤儿实体也将被删除。我阅读了相关段落 在文档中,甚至尝试使用 JDO 和 @Element(dependent = "false") 但问题仍然存在。我怎样才能让孤儿保持这种关系呢?
I have the following two entities:
@Entity
public class SupermarketChain {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String key;
@OneToMany(mappedBy = "supermarketChain")
@Basic
private List<Supermarket> supermarkets;
}
@Entity
public class Supermarket {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String key;
@ManyToOne(optional=true)
private SupermarketChain supermarketChain;
}
When I'm deleting a parent with em.remove(SupermarketChain.class, key), all orphans will be deleted too. I read the relevant paragraph in the documentation, even tried it with JDO with @Element(dependent = "false") but the problem remains. How can I retain the orphans in that relation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
留住孤儿是没有意义的。在 GAE JDO/JPA v1 中,所有关系都是“拥有的”,因此您必须有任何孩子的父母。如果父级不再存在,则子级将被删除。总是。
在 GAE JDO/JPA v2 中,您还可以拥有无主对象,因此没有“父对象”,因此它们可以在之后继续存在。
Retaining an orphan makes no sense. In v1 of GAE JDO/JPA all relations are "owned" so you have to have a parent of any child. And if the parent no longer exists then the child is deleted. Always.
In v2 of GAE JDO/JPA you will also be able to have unowned objects, hence there is no "parent" and so they can continue to exist after.