2012-11-08 66 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; 
    } 
}