问题
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)
回答1:
The creation of the Entities is not correct. You dont have to create a key on your own. If you create a Entity with a kind and keyname or ID parameter, a key will be created automaticlly out of the kind and the keyname or ID.
Entity customer = new Entity("Customer", "your name");
customer.setProperty("email", "your email");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(customer);
That is all you need to create the Entity you want.
When you give a kind and a key, a child Entity will be created. So you had created a Customer with "your name" who is a Customer with the ID 1.
To get the Entity with a key you just have to take the same parameters to create a key.
Key key = KeyFactory.createKey("Customer", "your name");
I would prefer to use get(), but i dont know what you want to do. For Example to get the email of a Customer:
Entity e;
try {
e = datastore.get(key);
String myEmail= (String) e.getProperty("your email");
回答2:
I think you want to use get() not query() when you have a Key.
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/DatastoreService#get(com.google.appengine.api.datastore.Key)
回答3:
Let us assume that you have a Java entity class called Product
and you wish to retrieve a unique record given its key i.e. ID
The method would look something like this:
public Product getProduct(Long id) {
//Get Persistence Manager
PersistenceManager pm = ...;
Key k = KeyFactory.createKey(Product.class.getSimpleName(), id);
Product product = null;
try {
product = pm.getObjectById(Product.class, k);
}
finally {
pm.close();
}
return product;
}
So in your example, I think you can replace Product
with your class name. I guess your class name is Customer
来源:https://stackoverflow.com/questions/14368995/how-to-select-a-single-entity-by-key-low-level-java-datastore-api