在Google App Engine应用中,我有这个模型:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Message {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private Date timestamp;
@Persistent
private String text;
@Unowned
@Persistent(defaultFetchGroup = "true")
private User sender;
...
}
该模型与发件人具有 @Unowned
关系,因为用户可以独立于邮件而存在 .
我想做的是持有 Message
对象与部分 User
对象(例如我存储消息就好了,但是,如果我没有't include all fields for the given user in the relationship, the user object is updated with the fields missing (e.g. user in question no longer has a password etc.). What is the best way of achieving what I want, without '腐败'原始对象?
PS我的 endpoints 方法很简单 . 基本上只是在消息上调用 pm.makePersistent(message);
(作为方法参数给出) .
1 回答
您正在使用
@Unowned
注释,这意味着只有对实际User
实体的引用将存储在Message
类下的sender
变量中 .当您访问
sender
变量时,数据存储区将执行get查询以检索链接到该消息的User
实体 .您可以通过导航到项目的数据存储仪表板(https://console.cloud.google.com/datastore用于 生产环境 ,http://localhost:8080/_ah/admin用于本地)并浏览
Message
实体来自行确认 .您应该看到
User
存储的字段名为user_id_OID
,其值为Key(User/XXXXX)
.作为旁注,Google建议从JDO / JPA转向Objectify或Datastore API .
看这里:https://cloud.google.com/appengine/docs/java/datastore/jdo/overview-dn2