Google Objectify v4 fetch by id

I am trying to get an object in App Engine with Objectify v4, but it does not work.

  • My @Entity: Translation.class
  • @Id @Entity I want to get: 301L

My @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; } } 

A query that does not contain a word:

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

But if I do this:

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

Then:

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

Thus, fetching by id does not work. Where is the problem?

+4
source share
2 answers

Since your object has an @Parent field, to get it by id, you need to do:

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

See Objectify Basic Operations - Download for more information.

Hope this helps!

+5
source

for @stickfigure, this is my real @Entity (PartOfSpeechGroup is also @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; } } 
0
source

Source: https://habr.com/ru/post/1444714/


All Articles