@Override public TruthValue matches(SQLConverter<Entity, Object> selector, ArrayMap<Table<Entity, Object>, Entity> rowCandidate) { Entity ae = rowCandidate.get(ancestor); Entity de = rowCandidate.get(descendant); if (isAncestorOf(ae.getKey(), de.getParent())) { return TruthValue.TRUE; } else { return TruthValue.FALSE; } }
@Override public Object get(Entity r, String property) { if (Entity.KEY_RESERVED_PROPERTY.equalsIgnoreCase(property)) { return r.getKey(); } if (PARENT.equalsIgnoreCase(property)) { return r.getParent(); } if (ID.equalsIgnoreCase(property)) { return r.getKey().getId(); } if (NAME.equalsIgnoreCase(property)) { return r.getKey().getName(); } Object ob = r.getProperty(property); if (ob instanceof RawValue) { RawValue rw = (RawValue) ob; return rw.getValue(); } return ob; }
private void storePKNameField(int fieldNumber, String value) { // TODO(maxr) make sure the pk is an encoded string AbstractMemberMetaData mmd = getMetaData(fieldNumber); if (!mmd.getType().equals(String.class)) { throw new NucleusFatalUserException( "Field with \"" + DatastoreManager.PK_ID + "\" extension must be of type String"); } Key key = null; if (value != null) { key = KeyFactory.createKey(datastoreEntity.getParent(), datastoreEntity.getKind(), value); } storeKeyPK(key); }
private Object lookupParent(AbstractMemberMetaData mmd, JavaTypeMapping mapping, boolean allowNullParent) { Key parentKey = datastoreEntity.getParent(); if (parentKey == null) { if (!allowNullParent) { String childClass = getObjectProvider().getClassMetaData().getFullClassName(); throw new NucleusFatalUserException("Field " + mmd.getFullFieldName() + " should be able to " + "provide a reference to its parent but the entity does not have a parent. " + "Did you perhaps try to establish an instance of " + childClass + " as " + "the child of an instance of " + mmd.getTypeName() + " after the child had already been " + "persisted?"); } else { return null; } } return mapping.getObject(ec, parentKey, NOT_USED); }
/** * Method to make sure that the Entity has its parentKey assigned. * Will update the Entity of this StoreFieldManager as necessary for the parent key. * Returns the assigned parent PK (when we have a "gae.parent-pk" field/property in this class). * @return The parent key if the pojo class has a parent property. Note that a return value of {@code null} * does not mean that an entity group was not established, it just means the pojo doesn't have a distinct * field for the parent. */ Object establishEntityGroup() { Key parentKey = datastoreEntity.getParent(); if (parentKey == null) { KeyRegistry keyReg = KeyRegistry.getKeyRegistry(ec); if (keyReg.isUnowned(getObjectProvider().getObject())) { return null; } parentKey = EntityUtils.getParentKey(datastoreEntity, getObjectProvider()); if (parentKey != null) { datastoreEntity = EntityUtils.recreateEntityWithParent(parentKey, datastoreEntity); } } AbstractMemberMetaData parentPkMmd = ((DatastoreManager)getStoreManager()).getMetaDataForParentPK(getClassMetaData()); if (parentKey != null && parentPkMmd != null) { return parentPkMmd.getType().equals(Key.class) ? parentKey : KeyFactory.keyToString(parentKey); } return null; }
assertEquals(pKey, child.getParent());
@Test public void testAncestor() { Key pKey = getParent().getKey(); Query query = new Query(CHILDKIND, pKey); assertEquals(2, service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults())); for (Entity cRec : service.prepare(query).asIterable()) { assertEquals(pKey, cRec.getParent()); } }
@Test public void testAncestorKey() { Key pKey = getParent().getKey(); Query query = new Query(CHILDKIND, pKey); query.addSort("__key__"); assertEquals(2, service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults())); for (Entity cRec : service.prepare(query).asIterable()) { assertEquals(pKey, cRec.getParent()); } }
Key parentKey = entity.getParent(); if (parentKey != null) { return parentKey;
service.put(entity); assertEquals("2", entity.getKey().getName()); assertEquals(pKey, entity.getParent());