2012-11-08 86 views
4

我想用Objectify v4獲取App Engine中的實體,但它不起作用。谷歌Objectify v4獲取ID

  • 我@Entity:Translation.class
  • 將在@Entity的@Id我想獲取:301L

我@Entity:

@Entity 
public class Translation { 
    @Id 
    private Long id; 
    private String text; 

    public String getText() { 
    return text; 
    } 

    public Long getId() { 
    return id; 
    } 

    public void setText(String text) { 
    this.text = text; 
    } 
} 

無字的請求:

Translation translation =ObjectifyService.ofy().load().type(Translation.class).id(301L).get(); // translation is null 

但是,如果我做的:

Translation translation = ObjectifyService.ofy().load().type(Translation.class).first().get(); // translation is not null 

然後:

System.out.println(translation.getId()); // translation id equal 301 

所以獲取由ID似乎並沒有工作。 問題在哪裏?

+1

這在測試用例中正常工作。你確定沒有更多的東西沒有展示給我們,比如實體中的@Parent字段? – stickfigure

+0

@stickfigure YES,true,我在** **實體**中有一個** Parent **字段。它有什麼變化? \ @Parent private Key partOfSpeechEntryKey; –

+0

請仔細閱讀,特別是關於Keys的部分:https://code.google.com/p/objectify-appengine/wiki/Concepts – stickfigure

回答

5

因爲你的實體有@Parent領域,爲了通過ID得到它,你需要執行:

Translation translation = ObjectifyService.ofy().load().type(Thing.class).parent(par).id(301).get(); 

欲瞭解更多信息,看看Objectify Basic Operations - Loading

希望這有助於!

+0

是的,但它不起作用。 THX –

+0

如果您嘗試使用實體的密鑰進行加載,會發生什麼情況?例如'Translation translation = ObjectifyService.ofy()。load().key(myKey).get();'另外,你可以添加你存儲實體的代碼嗎? –

+0

所以在這裏我翻譯表的App Engine中的內容概述:[IMG](http://i45.tinypic.com/2zq9eep.jpg) 然後, 重點 translationKey = ObjectifyService.ofy()負載( ).TYPE(Translation.class)。首先()信息getKey(); 翻譯翻譯= ObjectifyService.ofy().load().key(translateKey).get(); // Not null **因此請求的關鍵作品,但不是由Id ** 而下面我如何存儲我的@Entity Translation translation = new Translation(); translation.setText(「my string」); ()。save()。entity(translation).now(); THX –

0

for @stickfigure,這是我的真實@Entity(PartOfSpeechGroup顯然也是@Entity)。

@Entity 
public class Translation implements IsSerializable { 
    @Id 
    private Long id; 
    private String text; 
    @Parent 
    private Key<PartOfSpeechGroup> partOfSpeechEntryKey; 

    public Translation() {} 

    public Translation(Key<PartOfSpeechGroup> partOfSpeechEntryKey) { 
    this.partOfSpeechEntryKey = partOfSpeechEntryKey; 
    } 

    public String getText() { 
    return text; 
    } 

    public Long getId() { 
    return id; 
    } 

    public Key<PartOfSpeechGroup> getPartOfSpeechEntryKey() { 
    return partOfSpeechEntryKey; 
    } 

    public void setText(String text) { 
    this.text = text; 
    } 
}