Reputation: 2066
i have this code to check if a child entity already exists in datastore. The problem is that it works for one parent-child relation but always returns null for another relation.
i have four kind of entities in my app - ORG, Contact, Profile, ProfileOrg. Contact kind entities are children of ORG and ProfileOrg are children of Profile. The logic to add a contact to ORG is to get all childentities for the given org and then check for a property. if property matches i return the entity else null. this works perfectly for ORG-Contact relation. but the exact same logic for Profile-ProfileOrg relation doesnot work. it always returns null. Below is my code:
This is the code to get all childentities and check if its property "orgID" matches or not.
public static Entity getSingleProfileOrg(Key profileKey, String orgID) {
Iterable<Entity> childProfileOrgs = Util.listChildren("profileOrg",profileKey);
for (Entity e : childProfileOrgs) {
if (e.getProperty("orgID").toString() == orgID) {
return e;
}
}
return null;
}
This is the code to query datastore.
public static Iterable<Entity> listChildren(String kind, Key ancestor) {
logger.log(Level.INFO, "Search entities based on parent");
Query query = new Query(kind);
query.setAncestor(ancestor);
query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, ancestor);
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
Please help. this has got me stuck.
Edit: Code for getting ChildEntities for org which works perfectly
public static Entity getSingleContact(Key orgKey, String phoneContactID) {
Iterable<Entity> childContacts = Util.listChildren("contact", orgKey);
for (Entity e : childContacts) {
if (e.getProperty("contactID") == phoneContactID) {
return e;
}
}
return null;
}
Edit: Code to create or update ProfileOrg
public static void createOrUpdateProfileOrg(String profileID,
String orgName, String ownerID) {
String orgID = ownerID + "_" + orgName;
Entity Profile = Product.getProfile(profileID);
//Key profileKey = KeyFactory.createKey("Profile", profileID);
Key profileKey = Profile.getKey();
Entity profileOrg = getSingleProfileOrg(profileKey, orgID);
if (profileOrg == null) {
profileOrg = new Entity("profileOrg", profileKey);
profileOrg.setProperty("name", orgName);
profileOrg.setProperty("orgID", orgID);
profileOrg.setProperty("owner", ownerID);
profileOrg.setProperty("profileID", profileID);
Util.persistEntity(profileOrg);
}
}
the line
Entity Profile = Product.getProfile(profileID);
should be working fine as it is used to create profiles also and i dont see any problem there.
As i always get a null from getSingleProfileOrg(profileKey, orgID); i end up getting duplicate child entities.
Upvotes: 1
Views: 911
Reputation: 3115
Since in listChildren
, you want to query based an ancestor, your method is wrong. It should be like this:
public static Iterable<Entity> listChildren(String kind, Key ancestor) {
logger.log(Level.INFO, "Search entities based on parent");
Query query = new Query(kind);
query.setAncestor(ancestor);
PreparedQuery pq = datastore.prepare(query);
return pq.asIterable();
}
Hope this helps.
Upvotes: 1