如何在 JDO (Google App Engine) 中插入子实体?
如何向下面示例中的子实体添加记录?例如 我有一份员工记录,名字是“Sam”。我如何为 sam 添加 2 个街道地址?
猜猜我有一个
父实体是员工
import java.util.List;
// ...
@Persistent(mappedBy = "employee")
private List<ContactInfo> contactInfoSets;
子键是地址
import com.google.appengine.api.datastore.Key;
// ... imports ...
@PersistenceCapable
public class ContactInfo {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String streetAddress;
// ...
}
How do i add a record to a child entity in the example below ? For example
i have a Employee Record which is name is "Sam". how do i add 2 street adress for sam ?
Guess i have a
The Parent entity is Employee
import java.util.List;
// ...
@Persistent(mappedBy = "employee")
private List<ContactInfo> contactInfoSets;
The Child key is Adress
import com.google.appengine.api.datastore.Key;
// ... imports ...
@PersistenceCapable
public class ContactInfo {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String streetAddress;
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只是有效:
员工是:
地址是:
使用 @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") 作为类级别注释。通常,除了键之外,您不需要注释任何其他字段,因此
List
上的@Persistent(mappedBy = "employee")
是不必要的。顺便提一句。我建议使用参数化集合。
It just works:
Employee being:
Address being:
Use
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
as the class level annotation. Usually you don't need to annotate any other fields but the key, so the@Persistent(mappedBy = "employee")
on theList
is unnecessary.Btw. I suggest using parametrized collections.
插入和检索子条目可以通过以下方式完成:
父类 Person
}
依赖子类 : Contacts
Inserting and retrieving Child Entries can be done in the following way:
Parent class Person
}
Dependent Child Class : Contacts