2009-11-01 33 views
0

我擁有一個一對多的關係的兩個對象之間的負載對象組:JDO,GAE:通過孩子的關鍵

@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class AccessInfo { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private com.google.appengine.api.datastore.Key keyInternal;  
    ...  
    @Persistent 
    private PollInfo currentState; 

    public AccessInfo(){} 

    public AccessInfo(String key, String voter, PollInfo currentState) { 
     this.voter = voter; 
     this.currentState = currentState; 
     setKey(key); // this key is unique in whole systme 
    } 

    public void setKey(String key) { 
     this.keyInternal = KeyFactory.createKey(
       AccessInfo.class.getSimpleName(), 
       key); 
    } 

    public String getKey() { 
     return this.keyInternal.getName(); 
    } 

@PersistenceCapable(identityType = IdentityType.APPLICATION) 
public class PollInfo 
{ 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key key; 

    @Persistent(mappedBy = "currentState") 
    private List<AccessInfo> accesses; 
    ... 

我創建的類PollInfo做出的一個實例它堅持不懈。沒關係。 但是然後我想通過AccessInfo鍵加載這個組,並且我收到異常NucleusObjectNotFoundException。 是否可以按小孩的密鑰加載小組?

+0

「密鑰」(對於AccessInfo)是一個包含12個字符的隨機字母數字字符串。 PersistenceManager pm = PMF.get()。getPersistenceManager(); \t \t Transaction tx = pm.currentTransaction(); \t \t嘗試{ \t \t \t tx.begin(); \t \t \t AccessInfo ai; \t \t \t嘗試{ \t \t \t \t AI = pm.getObjectById(AccessInfo.class,KeyFactory.createKey( AccessInfo.class.getSimpleName(), 鍵)); – Solvek

回答

0

也許如果你引用你的代碼來檢索AccessInfo對象,以及你如何得到你使用的「密鑰」?

+0

「密鑰」(對於AccessInfo)是一個包含12個字符的隨機字母數字字符串。 PersistenceManager pm = PMF.get()。getPersistenceManager(); Transaction tx = pm.currentTransaction();嘗試{tx.begin(); AccessInfo ai;嘗試{ai = pm.getObjectById(AccessInfo.class,KeyFactory.createKey(AccessInfo.class.getSimpleName(),key)); – Solvek

+0

當你堅持AccessInfo對象時,爲什麼不直接打印出「pm.getObjectId(ai)」?並在pm.getObjectById(id)調用中使用它?這樣你就可以確定擁有正確的鑰匙 – DataNucleus