Search in sources :

Example 56 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class SizeOperationValidatorTest method shouldAllowSizeOperatorForListTypesAndLongValues.

@Test
public void shouldAllowSizeOperatorForListTypesAndLongValues() {
    // given
    MappedClass mappedClass = new MappedClass(EntityWithListsAndArrays.class, new Mapper());
    MappedField mappedField = mappedClass.getMappedField("listOfIntegers");
    List<ValidationFailure> validationFailures = new ArrayList<ValidationFailure>();
    // when
    boolean validationApplied = SizeOperationValidator.getInstance().apply(mappedField, SIZE, 3L, validationFailures);
    // then
    assertThat(validationApplied, is(true));
    assertThat(validationFailures.size(), is(0));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 57 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class IgnoreFieldsAnnotationTest method processIgnoreFieldsAnnotations.

//remove any MappedField specified in @IgnoreFields on the class.
private void processIgnoreFieldsAnnotations() {
    for (final MappedClass mc : getMorphia().getMapper().getMappedClasses()) {
        final IgnoreFields ignores = (IgnoreFields) mc.getAnnotation(IgnoreFields.class);
        if (ignores != null) {
            for (final String field : ignores.value().split(",")) {
                final MappedField mf = mc.getMappedFieldByJavaField(field);
                mc.getPersistenceFields().remove(mf);
            }
        }
    }
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 58 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class DatastoreImpl method toDbObject.

private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) {
    final MappedClass mc = mapper.getMappedClass(ent);
    if (mc.getAnnotation(NotSaved.class) != null) {
        throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
    }
    DBObject dbObject = entityToDBObj(ent, involvedObjects);
    List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class);
    for (MappedField mappedField : versionFields) {
        String name = mappedField.getNameToStore();
        if (dbObject.get(name) == null) {
            dbObject.put(name, 1);
            mappedField.setFieldValue(ent, 1L);
        }
    }
    return dbObject;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) NotSaved(org.mongodb.morphia.annotations.NotSaved) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 59 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class IndexHelper method collectNestedIndexes.

private List<Index> collectNestedIndexes(final MappedClass mc, final List<MappedClass> parentMCs) {
    List<Index> list = new ArrayList<Index>();
    for (final MappedField mf : mc.getPersistenceFields()) {
        if (!mf.isTypeMongoCompatible() && !mf.hasAnnotation(Reference.class) && !mf.hasAnnotation(Serialized.class) && !mf.hasAnnotation(NotSaved.class) && !mf.isTransient()) {
            final List<MappedClass> parents = new ArrayList<MappedClass>(parentMCs);
            parents.add(mc);
            List<MappedClass> classes = new ArrayList<MappedClass>();
            MappedClass mappedClass = mapper.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass());
            classes.add(mappedClass);
            classes.addAll(mapper.getSubTypes(mappedClass));
            for (MappedClass aClass : classes) {
                for (Index index : collectIndexes(aClass, parents)) {
                    List<Field> fields = new ArrayList<Field>();
                    for (Field field : index.fields()) {
                        fields.add(new FieldBuilder().value(field.value().equals("$**") ? field.value() : mf.getNameToStore() + "." + field.value()).type(field.type()).weight(field.weight()));
                    }
                    list.add(new IndexBuilder(index).fields(fields));
                }
            }
        }
    }
    return list;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) NotSaved(org.mongodb.morphia.annotations.NotSaved) Reference(org.mongodb.morphia.annotations.Reference) ArrayList(java.util.ArrayList) Index(org.mongodb.morphia.annotations.Index) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Example 60 with MappedField

use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.

the class TestMapping method testToMongoObjectCorrectlyMapsSerializableFieldForIssue591.

@Test
public void testToMongoObjectCorrectlyMapsSerializableFieldForIssue591() {
    // given
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = new SerializableObject();
    MappedClass mc = new MappedClass(User.class, mapper);
    MappedField mf = mc.getMappedField("userObject");
    // when
    Object dbValue = mapper.toMongoObject(mf, null, user.userObject);
    Class<byte[]> byteArrayClass = byte[].class;
    // then
    assertThat(dbValue, is(instanceOf(byteArrayClass)));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Mapper(org.mongodb.morphia.mapping.Mapper) DBObject(com.mongodb.DBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

MappedField (org.mongodb.morphia.mapping.MappedField)68 MappedClass (org.mongodb.morphia.mapping.MappedClass)56 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)47 Mapper (org.mongodb.morphia.mapping.Mapper)46 BasicDBObject (com.mongodb.BasicDBObject)13 DBObject (com.mongodb.DBObject)8 List (java.util.List)7 Key (org.mongodb.morphia.Key)6 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 ValidationFailure (org.mongodb.morphia.query.validation.ValidationFailure)4 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 DBCollection (com.mongodb.DBCollection)2 Index (org.mongodb.morphia.annotations.Index)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 Version (org.mongodb.morphia.annotations.Version)2 MappingException (org.mongodb.morphia.mapping.MappingException)2 DBRef (com.mongodb.DBRef)1 WriteResult (com.mongodb.WriteResult)1