Search in sources :

Example 21 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class KeyConverter method decode.

@Override
public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
    if (o == null) {
        return null;
    }
    if (!(o instanceof DBRef)) {
        throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
    }
    DBRef ref = (DBRef) o;
    MappedField actualType = getActualType(optionalExtraInfo);
    final Class<?> keyType = actualType != null ? actualType.getConcreteType() : getMapper().getClassFromCollection(ref.getCollectionName());
    final Key<?> key = new Key<Object>(keyType, ref.getCollectionName(), ref.getId());
    return key;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBRef(com.mongodb.DBRef) Key(org.mongodb.morphia.Key)

Example 22 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class MorphiaKeyIterator method convertItem.

@Override
@SuppressWarnings("unchecked")
protected Key<T> convertItem(final DBObject dbObj) {
    Object id = dbObj.get(Mapper.ID_KEY);
    if (id instanceof DBObject) {
        Class type = getMapper().getMappedClass(getClazz()).getMappedIdField().getType();
        id = getMapper().fromDBObject(getDatastore(), type, (DBObject) id, getMapper().createEntityCache());
    }
    return new Key<T>(getClazz(), getCollection(), id);
}
Also used : DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject) Key(org.mongodb.morphia.Key)

Example 23 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class QueryValidatorTest method shouldNotAllowTypeThatDoesNotMatchKeyTypeValue.

@Test
public void shouldNotAllowTypeThatDoesNotMatchKeyTypeValue() {
    // expect
    MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
    MappedField mappedField = mappedClass.getMappedField("name");
    assertThat(QueryValidator.isCompatibleForOperator(mappedClass, mappedField, String.class, EQUAL, new Key<Number>(Integer.class, "Integer", new ObjectId()), new ArrayList<ValidationFailure>()), is(false));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) Key(org.mongodb.morphia.Key) Test(org.junit.Test)

Example 24 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class TestQuery method testGetByKeysHetero.

@Test
public void testGetByKeysHetero() {
    final Iterable<Key<Object>> keys = getDs().save(asList(new FacebookUser(1, "scott"), new Rectangle(1, 1)));
    final List<Object> entities = getDs().getByKeys(keys);
    assertNotNull(entities);
    assertEquals(2, entities.size());
    int userCount = 0;
    int rectCount = 0;
    for (final Object o : entities) {
        if (o instanceof Rectangle) {
            rectCount++;
        } else if (o instanceof FacebookUser) {
            userCount++;
        }
    }
    assertEquals(1, rectCount);
    assertEquals(1, userCount);
}
Also used : Rectangle(org.mongodb.morphia.testmodel.Rectangle) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UsesCustomIdObject(org.mongodb.morphia.TestMapper.UsesCustomIdObject) Key(org.mongodb.morphia.Key) FacebookUser(org.mongodb.morphia.TestDatastore.FacebookUser) Test(org.junit.Test)

Example 25 with Key

use of org.mongodb.morphia.Key in project morphia by mongodb.

the class QueryForSubtypeTest method testSubclassOfKeyShouldBeCompatibleWithFieldUser.

@Test
public void testSubclassOfKeyShouldBeCompatibleWithFieldUser() {
    MappedField user = jobMappedClass.getMappedField("owner");
    Key<User> anonymousKeySubclass = new Key<User>(User.class, "User", 212L) {
    };
    boolean compatible = isCompatibleForOperator(jobMappedClass, user, User.class, EQUAL, anonymousKeySubclass, new ArrayList<ValidationFailure>());
    assertThat(compatible, is(true));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Key(org.mongodb.morphia.Key) ValidationFailure(org.mongodb.morphia.query.validation.ValidationFailure) Test(org.junit.Test)

Aggregations

Key (org.mongodb.morphia.Key)25 DBObject (com.mongodb.DBObject)13 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)8 BasicDBObject (com.mongodb.BasicDBObject)7 MappedField (org.mongodb.morphia.mapping.MappedField)5 ProxiedEntityReference (org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference)5 DBRef (com.mongodb.DBRef)4 Map (java.util.Map)3 ObjectId (org.bson.types.ObjectId)3 FacebookUser (org.mongodb.morphia.TestDatastore.FacebookUser)3 MappedClass (org.mongodb.morphia.mapping.MappedClass)3 Mapper (org.mongodb.morphia.mapping.Mapper)3 ReflectionUtils.getParameterizedClass (org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass)3 BasicDBList (com.mongodb.BasicDBList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Datastore (org.mongodb.morphia.Datastore)2 KeysKeysKeys (org.mongodb.morphia.TestDatastore.KeysKeysKeys)2 Reference (org.mongodb.morphia.annotations.Reference)2