如何更新Ref'ed对象?

时间:2015-12-09 03:28:23

标签: java google-app-engine objectify

设计:

  • 实体A,B和C
  • C是B中的参考,B是Ref'd和A
  • 所有三个都是单独创建的
  • 检索A,B和C时也会检索

问题:  当我尝试保存更新的A时,我收到了......这个错误:

更新Sticky:400错误请求时收到IOException                                                                                                               {                                                                                                                 “代码”:400,                                                                                                                 “错误”:[                                                                                                                   {                                                                                                                     “域名”:“全球”,                                                                                                                     “message”:“com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException:您无法为具有null @Id的对象创建密钥。对象为com.example.entity_C@db12bd5(通过参考链) :com.example.entity_A [\“owner \”] - > com.example.entity_B [\“myC \”])“,                                                                                                                     “理由”:“badRequest”                                                                                                                   }                                                                                                                 ]                                                                                                                 “message”:“com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException:您无法为具有null @Id的对象创建密钥。对象为com.example.entity_C@db12bd5(通过参考链) :com.example.entity_A [\“owner \”] - > com.example.entity_B [\“myC \”])“                                                                                                               }

2 个答案:

答案 0 :(得分:0)

不幸的是,Java总是通过引用传递,意味着任何对象A = B,比你试图改变B,A也会改变,唯一的方法就是创建新的Object A = new Object();然后你将对象B的内容复制到对象A,将是这样的

int someInt = 5;
Object B = new Object(someInt);

其中Object类的构造函数采用" someInt"参数。

class Object {
int j;
 Object(Int i)
{
 this.j=i;
}
int getInt() 
{
  return j;
 }

然后创建A并将A的内容均衡为B的内容

  Object A = new Object(B.getInt());

答案 1 :(得分:0)

我终于解决了这个问题......显然,我的@Id字段没有set方法,因此客户端库自动生成并不是如何在客户端库结束时设置值。 / p>