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));
}
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);
}
}
}
}
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;
}
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;
}
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)));
}
Aggregations