繁体   English   中英

参考 <?> 值尚未初始化

[英]Ref<?> value has not been initialized

我收到这个错误:

java.lang.IllegalStateException: Ref<?> value has not been initialized
    at com.googlecode.objectify.impl.ref.StdRef.get(StdRef.java:55)
    at com.mycomp.simplify.KeyValueVersion.getVersion(KeyValueVersion.java:59)

当试图坚持这个实体时:

public class KeyValueVersion {


        @Id
        private Long id;
        private Ref<Key> key;
        private Ref<Value> value;
        private Ref<Version> version;

        public KeyValueVersion() {

        }

        public KeyValueVersion(Key key, Value value, Version version) {
            setKey(key);
            setValue(value);
            setVersion(version);
        }

        public Key getKey() {
            return this.key.get();
        }
        public void setKey(Key key) {
            this.key = Ref.create(key.getKey(), key);
        }
        public Value getValue() {
            return this.value.get();
        }
        public void setValue(Value value) {
            this.value = Ref.create(value.getKey(), value); 
        }
        public Version getVersion() {
            return this.version.get();
        }
        public void setVersion(Version version) {
            this.version = Ref.create(version.getKey(), version); 
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }
    }

这就是我坚持实体的方式:

public Version put(final Key key, final Value value) throws KeyException {
    final ExceptionWrapper ew = new ExceptionWrapper();
    Version v = ofy().transact(new Work<Version>() {
        @Override
        public Version run() {
            Version v = null;
            try {
                Version version = new Version(new Date().getTime());
                ofy().save().entity(key).now();
                ofy().save().entity(value).now();
                ofy().save().entity(version).now();
                com.googlecode.objectify.Key<KeyValueVersion> result = 
                        ofy().save().entity(new KeyValueVersion(key, value, version)).now();
                v = get(result).getVersion();
            } catch (EntityNotFoundException e) {
                ew.exception = new KeyPersistenceFailureException(key);
            } 
            return v;
        }
    }); 
    if(ew.exception != null) throw ew.exception;
    return v;
}

这是运行这些代码的主要测试:

@Test
public void testCreateFetch() throws KeyException { 
    Value val = Value.createValue("John".getBytes());
    Key key = Key.createKey("uid:john:fname");
    Version ver = sfy.put(key, val); 

}
  • KeyValueVersion的静态createXxx方法只是创建这些类的新实例
  • put()方法将实体保存到数据存储区中,然后将这些实体传递到KeyValueVersion ,然后保存到数据存储区中

问题是您正在获取KeyValueVersion实体,然后尝试访问Ref的值。 Objectify默认不加载引用,因此您尝试访问未初始化的Ref。

我不是很清楚你想要做什么,但如果你在Ref字段上添加@Load,Objectify会在你加载主实体时为你加载它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM