0

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

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
4

3 回答 3

2

您的实体类中有一个没有 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); }
}

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

于 2012-10-10T19:36:06.793 回答
1

对于要使用 Objectify 持久化的每个实体,您应该拥有一个带有 @Id 注释的 Long、long 或 String 类型的属性。这用作实体的密钥。有关详细信息,请查看Objectify 用户指南

希望有帮助!

于 2012-10-10T19:27:58.823 回答
0

我只是有这个确切的例外并解决了它。

如果您未能使用 now() 方法同步自动生成一个 Long Id,然后尝试使用该 Id,您可以获得确切的堆栈跟踪输出。

坏的

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

好的

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

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

于 2016-04-28T02:32:24.963 回答