-1

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. enter image description here

These are the Item Entities.

enter image description here

As you can see, this Item is simple, without any extra ancestor Keys. And you can see below it works.

enter image description here

And this is the one with the parents/acestors.

enter image description here

As you can see below, it doesn't work.

enter image description here

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
smashah
  • 307
  • 2
  • 10
  • 1
    Have you read this?: https://cloud.google.com/appengine/articles/scaling/contention?hl=en In particular, the part about keeping entity groups small. – new name Oct 10 '15 at 13:52
  • You'll need to show some code - how are you trying to get retrieve the `Item`? Also your question is unclear: you say *the Item entity has multiple ancestors* - it has *one* ancestor, a `Type`, which in turn has another ancestor. An entity can have only one direct ancestor. – tx802 Oct 10 '15 at 13:55
  • Also, this: http://stackoverflow.com/questions/33000904/ancestor-query-direct-descendants-google-datastore/33010769 – tx802 Oct 10 '15 at 13:57
  • ok I've had a look at the docs, I guess I was naive to think that I could introduce this level over ancestry without any problems @Kekito. – smashah Oct 10 '15 at 14:02
  • Thanks for the link to the other post @tx802 , It makes a good point, maybe I don't need so many levels above the actual item. The idea of having a single kind called parentCategory makes sense. Would I still be able to keep this level of ancestry and avoid the issues if they're all of the same kind? – smashah Oct 10 '15 at 14:03
  • @user2664050 I'm not sure what you're asking but ancestor keys (i) can be of any Kind and (ii) don't event have to exist as entities. – tx802 Oct 10 '15 at 14:06
  • I did not know (ii), I will try this again without making the ancestors into entities. @tx802 – smashah Oct 10 '15 at 14:12
  • ok That didn't work @tx802. It seems that if I have any ancestors, entity or not, on the item, the rating will not link back to the item. – smashah Oct 10 '15 at 14:20

1 Answers1

1

You have:

String target =  req.getParameter("target");                                               
Key k = KeyFactory.createKey("Item", Long.parseLong(target));

This will give you the key for an Item with a long id of whatever the value of target is. It is probably not the Item you are looking for.

Assuming your trgt is mean to be the Item in scenario one, you cannot retrieve it by id alone - you need the full key path of Category > Aisle > Group > Type > Item as IDs are unique only within an entity group.

Also, your raterkind will always be "Customer" - you want

if (type.equals("user")) {...}

instead of

if (type=="user") {...}
Community
  • 1
  • 1
tx802
  • 3,524
  • 2
  • 17
  • 22
  • Thanks for the == thing. I see what you're saying about getting the whole key. I made one Item property to show its own key and it came out to be Key('Category', 'FoodCupboard', 'Aisle', 'RPN', 'Group', 'Rice', 'Type', 'Basmati', 'Item', 5649050225344512) How do I get it to find the key from within any entity group? – smashah Oct 10 '15 at 14:46
  • I just ended up changing the item as the main Entity. Thanks for your help though. – smashah Oct 10 '15 at 15:13