【问题标题】:Error saving entity with Objectify使用 Objectify 保存实体时出错
【发布时间】:2012-10-01 07:30:51
【问题描述】:

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

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

【问题讨论】:

    标签: google-app-engine objectify


    【解决方案1】:

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

    希望有帮助!

    【讨论】:

      【解决方案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 时,您会立即得到一个明确的异常。

      【讨论】:

      • 我试过 'Ref.create(value)' 但它给出了一个错误,这就是你的意思:“Ref.create(value.getKey(), value)' 吗?
      • 如果这是执行 Ref.create 的正确方法,那么我该如何执行 getKey():stackoverflow.com/questions/12830906/…
      • 是的,使用 Ref.create(key, value)。 Ref.create(value) 方法尚未发布,但很快就会发布。
      【解决方案3】:

      我刚刚遇到了这个确切的异常并解决了它。

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

      不好

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

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

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

      【讨论】:

        猜你喜欢
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 2021-08-05
        • 2017-10-01
        相关资源
        最近更新 更多