首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Objectify保存实体时出错

使用Objectify保存实体时出错
EN

Stack Overflow用户
提问于 2012-10-11 02:27:10
回答 3查看 3K关注 0票数 0

这是我试图持久化一个实体时的错误:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-11 03:36:07

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

代码语言:javascript
复制
@Entity
class MyEntity {
    @Id Long id;
    OtherEntity other;   // this thing has a null id
}

Objectify无法保存OtherEntity的密钥,因为它无法创建密钥。

这就是为什么在Objectify的下一个版本中删除这种直接实体引用的功能的一个原因(几个)。您需要将代码更改为:

代码语言:javascript
复制
@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时,您将立即得到一个明显的异常。

票数 2
EN

Stack Overflow用户

发布于 2012-10-11 03:27:59

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

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2016-04-28 10:32:25

我正好有了这个例外,并解决了它。

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

错误的

代码语言:javascript
复制
ofy().save().entity(myEntity);
Ref.create(myEntity);

Good

代码语言:javascript
复制
ofy().save().entity(myEntity).now();
Ref.create(myEntity);

你可能遇到这个异常的原因有很多。忘记设置@Id注释不是其中之一。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12825930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档