2012-10-12 97 views
1

我得到這個錯誤:參考<?>值尚未初始化

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方法只需創建這些類
  • 之前,這些被傳遞到KeyValueVersion被然後保存到該put()方法保存實體到數據存儲的新新實例datasore

回答

3

問題是,您正在獲取您的KeyValueVersion實體,然後嘗試訪問Ref的值。 Objectify默認不加載引用,所以你試圖訪問未初始化的引用。

我並不清楚你想要做什麼,但是如果你在Ref字段上添加@Load,Objectify會在加載主實體時爲你加載它們。

+0

謝謝你,現在修好了 – xybrek