我可以在父类的@PrePersist处理程序中持久化子对象吗? (Objectify 3.1b1) [英] Can I persist child objects in @PrePersist handler of a parent class? (Objectify 3.1b1)
问题描述
我是Objectify的新手,试图实现一对多关系。我有实体组织和实体个人。组织有 @Transient
属性列表< Person> contactPeople 。 Class Person有 @Parent
property Key<组织> organizationKey ,我可以通过setter设置。
我想在中保留 contactPeople 组织的@PrePersist
处理程序。为了做到这一点,我需要在Person中设置父键。
Wiki 此处表示:您无法更新@PrePersist回调中的@Id或@Parent字段;此时,低级别实体已经用一个完整的Key构造,所以它可以作为可选参数传入。
我不确定这仍然是准确的信息吗?因为我在 PrePersist
处理程序中获取的 com.google.appengine.api.datastore.Entity
对象的键具有以下关键:字面意思是no-id-yet。
你会如何实现这一点?
谢谢! / p>
2011年11月17日更新:
在新的Objectify4中,能够做到这样的半自动关系:
class Beastie {
@Parent
@Load
ParentThing父母;
@Id Long id;
@Load({bigGroup,smallGroup})
SomeThing some;
@Load(bigGroup)
List< OtherThing>其他;
@Load
Ref< OtherThing> refToOtherThing;
Ref< OtherThing> anotherRef; //这个永远不会被自动获取
}
这里 是不断发展的新版本设计文档。
这是个大新闻。 Twig 作者John Patterson今天加入了Objectify项目。
嗯,似乎您需要在数据模型前制作一个Dao。因此,您可以执行以下操作:
组织机构= ...
列表< Person> people = ...
ob.put(organization)
for(Person person:people){
person.organizationKey = organization.getKey();
ob.put(person);
organization.contactPeopleKeys.add(person.getKey());
}
ob.put(组织)
GAE + Objectify需要很多的东西来处理你自己的代码,所以这是一件普通的事情。
I am new to Objectify and trying to implement One-to-Many relationship. I have entities Organization and entity Person. Organization has @Transient
property List< Person > contactPeople. Class Person has @Parent
property Key< Organization > organizationKey which I can set via setter.
I'd like to persist contactPeople in @PrePersist
handler of Organization. In order to do that I need to set parent key in Person.
Wiki here says: "You can't update @Id or @Parent fields in a @PrePersist callback; by this time, the low-level Entity has already been constructed with a complete Key so it can be passed in as an optional parameter."
I'm not sure this is still accurate information ? Because key of com.google.appengine.api.datastore.Entity
object that I get in PrePersist
handler has key that literally says "no-id-yet".
How would you implement this ?
Thank you!
Update at Nov 17, 2011:
In new Objectify4 we'll be able to do semi-automatic relationships like this:
class Beastie {
@Parent
@Load
ParentThing parent;
@Id Long id;
@Load({"bigGroup", "smallGroup"})
SomeThing some;
@Load("bigGroup")
List<OtherThing> others;
@Load
Ref<OtherThing> refToOtherThing;
Ref<OtherThing> anotherRef; // this one is never fetched automatically
}
Here is evolving design document of new version.
This is big news. Twig author, John Patterson, joined Objectify project today.
Hm, seems that you need to make an Dao in front of your data models. So, you will able to do something like:
Organization organization = ...
List<Person> people = ...
ob.put(organization)
for (Person person: people) {
person.organizationKey = organization.getKey();
ob.put(person);
organization.contactPeopleKeys.add(person.getKey());
}
ob.put(organization)
GAE+Objectify requires a lot of thing to handle by your own code, so it's a common thing
这篇关于我可以在父类的@PrePersist处理程序中持久化子对象吗? (Objectify 3.1b1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!