I'm trying to set an existing (Item) entity as the parent of another kind (rating) of entity. So I can let my users rate items.
At the moment the Item entity has an ancestor with an ancestor etc. of different kinds as shown below. In scenario 1 the Item entity cannot be found to be entered as a parent to the Rating. But in scenario 2 it works swimmingly.
Scenario 1 Scenario 2
Category
v
Aisle
v
Group
v
Type
v
Item >>>>> getKey() Item >>>>> getKey()
v v
Rating Rating
Please help! When the item has no ancestors it works fine, but when it has all the ancestors I need it to have it doesn't work!!!
Here's the code I'm using.
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
int rating = Integer.parseInt(req.getParameter("rating"));
String type = req.getParameter("type");
String raterid = req.getParameter("rater");
String target = req.getParameter("target");
Key k = KeyFactory.createKey("Item", Long.parseLong(target));
Entity trgt = null;
try {
trgt = datastoreService.get(k);
} catch (EntityNotFoundException e) {
e.printStackTrace();
}
Key k = KeyFactory.("Item", target);
Key r;
String raterkind;
if (type=="user"){
raterkind = "Deliverer";
}
else {raterkind = "Customer";}
r = KeyFactory.createKey(raterkind,raterid);
Entity ratingentity = null;
if (trgt != null) {
ratingentity = new Entity("rating",trgt.getKey());
}
else {
ratingentity = new Entity("rating",k);
}
ratingentity.setProperty("target",k);
ratingentity.setProperty("rating",rating);
ratingentity.setProperty("rater",r);
datastoreService.put(ratingentity);
Here are some pictures to show what's happening.
These are the Item Entities.
As you can see, this Item is simple, without any extra ancestor Keys. And you can see below it works.
And this is the one with the parents/acestors.
As you can see below, it doesn't work.