Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 使用Objectify保存实体时出错_Google App Engine_Objectify - Fatal编程技术网

Google app engine 使用Objectify保存实体时出错

Google app engine 使用Objectify保存实体时出错,google-app-engine,objectify,Google App Engine,Objectify,这是我尝试持久化实体时出现的错误: com.googlecode.objectify.SaveException: Error saving com.mycomp.simplify.KeyValueVersion@60991f: You cannot create a Key for an object with a null @Id. Object was com.mycomp.simplify.Key@1bf3d87 at com.googlecode.objectify.impl.

这是我尝试持久化实体时出现的错误:

com.googlecode.objectify.SaveException: Error saving com.mycomp.simplify.KeyValueVersion@60991f: You cannot create a Key for an object with a null @Id. Object was com.mycomp.simplify.Key@1bf3d87
    at com.googlecode.objectify.impl.Transmog.save(Transmog.java:102)
    at com.googlecode.objectify.impl.Concret

> Blockquote

eEntityMetadata.save(ConcreteEntityMetadata.java:150)
    at com.googlecode.objectify.impl.engine.WriteEngine.save(WriteEngine.java:68)
    at com.googlecode.objectify.impl.cmd.SaverImpl.entities(SaverImpl.java:56)
    at com.googlecode.objectify.impl.cmd.SaverImpl.entity(SaverImpl.java:33)
    at com.mycomp.simplify.core.ObjectifyGenericDao.put(ObjectifyGenericDao.java:52)
    at com.mycomp.simplify.SimplifyImpl.access$0(SimplifyImpl.java:1)
    at com.mycomp.simplify.SimplifyImpl$1.run(SimplifyImpl.java:43)
    at com.mycomp.simplify.SimplifyImpl$1.run(SimplifyImpl.java:1)
    at com.googlecode.objectify.impl.cmd.ObjectifyImpl.transactOnce(ObjectifyImpl.java:255)
    at com.googlecode.objectify.impl.cmd.ObjectifyImpl.transactNew(ObjectifyImpl.java:232)
    at com.googlecode.objectify.impl.cmd.ObjectifyImpl.transactNew(ObjectifyImpl.java:222)
    at com.googlecode.objectify.impl.cmd.ObjectifyImpl.transact(ObjectifyImpl.java:214)
    at com.googlecode.objectify.util.cmd.ObjectifyWrapper.transact(ObjectifyWrapper.java:165)
    at com.mycomp.simplify.SimplifyImpl.put(SimplifyImpl.java:37)
    at com.mycomp.simplify.ResourceTest.testCreateFetch(ResourceTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: You cannot create a Key for an object with a null @Id. Object was com.mycomp.simplify.Key@1bf3d87
    at com.googlecode.objectify.impl.translate.EntityClassTranslator.getRawKey(EntityClassTranslator.java:160)
    at com.googlecode.objectify.ObjectifyFactory.getRawKey(ObjectifyFactory.java:270)
    at com.googlecode.objectify.impl.translate.EntityReferenceTranslatorFactory$1.saveValue(EntityReferenceTranslatorFactory.java:39)
    at com.googlecode.objectify.impl.translate.EntityReferenceTranslatorFactory$1.saveValue(EntityReferenceTranslatorFactory.java:1)
    at com.googlecode.objectify.impl.translate.ValueTranslator.savePropertyValue(ValueTranslator.java:51)
    at com.googlecode.objectify.impl.translate.PropertyValueNodeTranslator.saveAbstract(PropertyValueNodeTranslator.java:38)
    at com.googlecode.objectify.impl.translate.AbstractTranslator.save(AbstractTranslator.java:38)
    at com.googlecode.objectify.impl.TranslatableProperty.executeSave(TranslatableProperty.java:145)
    at com.googlecode.objectify.impl.translate.ClassTranslator.saveMap(ClassTranslator.java:94)
    at com.googlecode.objectify.impl.translate.MapNodeTranslator.saveAbstract(MapNodeTranslator.java:38)
    at com.googlecode.objectify.impl.translate.AbstractTranslator.save(AbstractTranslator.java:38)
    at com.googlecode.objectify.impl.Transmog.saveToNode(Transmog.java:109)
    at com.googlecode.objectify.impl.Transmog.save(Transmog.java:96)
    ... 39 more

对于要使用Objectify持久化的每个实体,都应该有一个类型为Long、Long或带有@Id注释的String的属性。这被用作实体的键。有关详细信息,请参阅


希望有帮助

对于要使用Objectify持久化的每个实体,都应该有一个类型为Long、Long或String且带有@Id注释的属性。这被用作实体的键。有关详细信息,请参阅


希望有帮助

您的实体类中有一个实体引用,它没有id。也就是说,类似这样:

@Entity
class MyEntity {
    @Id Long id;
    OtherEntity other;   // this thing has a null id
}
Objectify无法为OtherEntity保存密钥,因为它无法创建密钥

这是Objectify的下一版本中删除直接实体引用功能的原因之一。您需要将代码更改为:

@Entity
class MyEntity {
    @Id Long id;
    Ref<OtherEntity> other;
    public OtherEntity getOther() { return other.get(); }
    public void setOther(OtherEntity value) { this.other = Ref.create(value); }
}
@实体
类实体{
@Id长Id;
参考其他;
public OtherEntity getOther(){return other.get();}
public void setOther(OtherEntity值){this.other=Ref.create(value);}
}

此模式消除了混淆,因为当您尝试创建无效引用时,会立即出现一个明确的异常。

您的实体类中有一个没有id的实体引用。也就是说,类似这样的情况:

@Entity
class MyEntity {
    @Id Long id;
    OtherEntity other;   // this thing has a null id
}
Objectify无法为OtherEntity保存密钥,因为它无法创建密钥

这是Objectify的下一版本中删除直接实体引用功能的原因之一。您需要将代码更改为:

@Entity
class MyEntity {
    @Id Long id;
    Ref<OtherEntity> other;
    public OtherEntity getOther() { return other.get(); }
    public void setOther(OtherEntity value) { this.other = Ref.create(value); }
}
@实体
类实体{
@Id长Id;
参考其他;
public OtherEntity getOther(){return other.get();}
public void setOther(OtherEntity值){this.other=Ref.create(value);}
}

这种模式消除了混淆,因为当您尝试创建无效引用时,会立即得到一个明确的异常。

我刚刚遇到了这个异常并解决了它

如果无法使用now()方法同步自动生成一个长Id,然后尝试使用该Id,则可以获得确切的stacktrace输出

坏的

ofy().save().entity(myEntity);
Ref.create(myEntity);
ofy().save().entity(myEntity).now();
Ref.create(myEntity);

ofy().save().entity(myEntity);
Ref.create(myEntity);
ofy().save().entity(myEntity).now();
Ref.create(myEntity);

您可能遇到该异常的原因有多种。忘记设置@Id注释不是其中之一。

我刚刚遇到了这个异常,并解决了它

如果无法使用now()方法同步自动生成一个长Id,然后尝试使用该Id,则可以获得确切的stacktrace输出

坏的

ofy().save().entity(myEntity);
Ref.create(myEntity);
ofy().save().entity(myEntity).now();
Ref.create(myEntity);

ofy().save().entity(myEntity);
Ref.create(myEntity);
ofy().save().entity(myEntity).now();
Ref.create(myEntity);

您可能遇到该异常的原因有多种。忘记设置@Id注释不是其中之一。

我尝试了“Ref.create(value)”,但它给出了一个错误,这就是您的意思:“Ref.create(value.getKey(),value)”吗?如果这是执行Ref.create的正确方法,那么我如何执行getKey():是的,使用Ref.create(key,value)。Ref.create(value)方法尚未发布,但很快就会发布。我尝试了“Ref.create(value)”,但它给出了一个错误,您的意思是:“Ref.create(value.getKey(),value)”吗?如果这是执行Ref.create的正确方法,那么如何执行getKey():是的,使用Ref.create(key,value)。Ref.create(value)方法尚未发布,但很快就会发布。