I'm a bit confused on how to create a "Key" object to select exactly 1 row of my Entity ("Customer").
My code :
Query query = new Query("Customer");
// **** how do I have to create this key ???
Key key = KeyFactory.createKey("Customer", 1);
// ****
FilterPredicate keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL, key);
query.setFilter(keyFilter);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
PreparedQuery pq = datastore.prepare(query);
Entity customer = pq.asSingleEntity();
if (! (customer == null)) {
log.info("OK !");
}
else {
log.info("no customer found");
}
My result is always : "no customer found".
With the datastore viewer (I'm working locally) I can see 3 rows :
- id/name = 1 email = "your email" name = "your name"
- id/name = 2 email = "your email" name = "your name"
- id/name = 3 email = "your email" name = "your name"
I want to select customer with id/name=1. I've tried : KeyFactory.createKey("Customer", 1); and KeyFactory.createKey("Customer", "your name");
but with no success.
When I do programmatically a search (asList) on "Customer" and I print the keys' values I see :
- row 1 key = Customer("your name")/Customer(1)
- row 2 key = Customer("your name")/Customer(2)
- row 3 key = Customer("your name")/Customer(3)
The printable values of the keys are in the format : "Entity(name)/Entity(id/name)"
How can I create such key values in my code ? On the javadoc I see only :
- createKey("kind", id)
- createKey("kind", "name")
The other methods require an ancestor, I've not created an ancestor .... This is the code that I've executed to created the 3 rows (it has been executed 3 times) :
Key customerKey = KeyFactory.createKey("Customer", "your name");
Entity customer = new Entity("Customer", customerKey);
customer.setProperty("name", "your name");
customer.setProperty("email", "your email");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(customer);
Thanks in advance !
Regards
Max
In other words, how do create a Key whose "toString" gives the following result ?
- Code : log.info("customer KeyFactory.keyToString = "+KeyFactory.keyToString(key));
- Output : INFO: customer KeyFactory.keyToString = Customer("your name")/Customer(1)