Google App Engine datastore request with a key.

I am deploying a simple Java application for the Google App Engine.

I have a simple JPA object containing the key as my generated identifier.

import javax.persistence.*; @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private com.google.appengine.api.datastore.Key key; ... 

As soon as I saved this object. I can view the key id, for example ...

 long id = entity.getKey().getId(); 

Do you know how I can use the same identifier to return my entity? Something like that...

 Query query = em.createQuery("SELECT e FROM MyEntity e WHERE e.key.id = :myId"); query.setParameter("myId", id); 

The above does not work. I know that I can return it by passing the key as a parameter, but I would like to know if I can use a long identifier.

+4
source share
4 answers

Found solution, create a key using KeyFactory and go to the identifier. Then Query on Key.

 Key key = KeyFactory.createKey(MyEntity.class.getSimpleName(), id); 
+11
source

There is no need to make a request at all, since the data warehouse initially supports fetching an object by its key, which is significantly faster than query execution. See this section of the docs.

+5
source

You probably understood this many years ago, but for others this is the way I do it in JPA:

 entityManager.find(Reply.class, key) 
0
source

In the data warehouse developer admin console, you can say something like:

SELECT * FROM MyEntity WHERE __key__ = Key(MyEntity, 5695872079757312)

0
source

All Articles