How to get Google Appengine objects by ID (long value)?

i declared the object as follows:

public class MyEntity {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Long id;
 @Persistent
 private String text;
        //getters and setters
}

Now I want to get objects using id. I tried to manage it from the Google Appengine Data Viewer using "SELECT * FROM MyEntity Where id = 382005" or using a request in the servlet. I am not getting any results. But I know for sure that an object with an identifier exists (I created jsp that requests all the objects in db and displays them in db).

So what is wrong with my request? Am I requesting the wrong field? The Google Appengine Data Viewer names the field "ID / name" and has the value "id = 382005". Should I request these names? I tried, but it did not work :(

+3
1

, :

MyEntity yourEntity = entityManager.find(MyEntity.class, yourId);

, , find(), :

Query query = entityManager.createQuery(
    "SELECT m FROM MyEntity m WHERE id = :id");
query.setParameter("id", yourId);

MyEntity yourEntity = (MyEntity) query.getSingleResult();
+4

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


All Articles