2014-01-08 83 views
2

我仍然無法使用GAE的keys/id。我不斷收到錯誤信息:找不到與該鍵匹配的實體:鍵(Medewerker(5201690726760448))。這些實體存在於數據存儲中,我多次檢查過。通過ID檢索用戶對象

我想只是簡單地得到一個用戶對象與一個特定的ID。在我的servlet中,我有以下代碼:

Long userId = Long.parseLong(req.getParameter("user")); 
User user = userDao.getUser(userId); 

上面的代碼會引發錯誤。在userDaoOfyImpl.java我有以下方法「的getUser」:

public Gebruiker getGebruiker(Long id) { 
    Gebruiker result = null; 
    Gebruiker leerling = (Gebruiker) ofy.get(Leerling.class, id); 
    Gebruiker medewerker = (Gebruiker) ofy.get(Medewerker.class, id); 
    Gebruiker stagebedrijf = (Gebruiker)ofy.get(StageBedrijf.class, id); 

    //Gebruiker instantie returnen 
    if(leerling != null) { 
     result = leerling; 
    } else if(medewerker != null) { 
     result = medewerker; 
    } else if(stagebedrijf != null) { 
     result = stagebedrijf; 
    } 

    return result; 
} 

的變量是荷蘭人,但我想你們知道的想法。上面的方法在不同的類中搜索,尋找匹配ID的用戶,然後返回它。

問題是我得到上面顯示的錯誤,我真的很沮喪,我做錯了什麼傢伙?它是使用ID還是...的方法或方式?

在此先感謝!

回答

1

here你可以閱讀的get方法:

Throws: NotFoundException - if the key does not exist in the datastore

使用

Gebruiker leerling = (Gebruiker) ofy.find(Leerling.class, id); 

find方法不拋出NotFoundException當該鍵不存在,但null

+0

Mateusz感謝您的回答,我認爲它的作品,但它留給我以下問題。當我嘗試添加創建的對象時,我得到一個異常「leerling不是受支持的屬性類型」,但我試圖添加對象: CompetentieLijst cL = new CompetentieLijst(leerling,today,false); ofy.put(cL); – JasonK

+0

檢查[this](http://stackoverflow.com/questions/5428571/objectify-appengine-embedded-class-not-a-supported-property-type)。如果這沒有幫助,請提供更多細節。 – Mateusz