Search in sources :

Example 71 with MappedClass

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

the class ExternalMapperExtTest method testExternalMapping.

@Test
public void testExternalMapping() throws Exception {
    final Mapper mapper = getMorphia().getMapper();
    final CloneMapper helper = new CloneMapper(mapper);
    helper.map(Skeleton.class, EntityWithNoAnnotations.class);
    final MappedClass mc = mapper.getMappedClass(EntityWithNoAnnotations.class);
    mc.update();
    assertNotNull(mc.getIdField());
    assertNotNull(mc.getEntityAnnotation());
    assertEquals("special", mc.getEntityAnnotation().value());
    EntityWithNoAnnotations ent = new EntityWithNoAnnotations();
    ent.id = "test";
    final Key<EntityWithNoAnnotations> k = getDs().save(ent);
    assertNotNull(k);
    ent = getDs().get(EntityWithNoAnnotations.class, "test");
    assertNotNull(ent);
    assertEquals("test", ent.id);
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 72 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass 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 73 with MappedClass

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

the class TestVersionAnnotation method testCanMapAPackageContainingAVersionedAbstractBaseClass.

@Test
public void testCanMapAPackageContainingAVersionedAbstractBaseClass() {
    Morphia morphia = getMorphia().mapPackage("org.mongodb.morphia.entities.version");
    Collection<MappedClass> mappedClasses = morphia.getMapper().getMappedClasses();
    assertThat(mappedClasses.size(), is(2));
    List<Class<?>> list = new ArrayList<Class<?>>();
    for (MappedClass mappedClass : mappedClasses) {
        list.add(mappedClass.getClazz());
    }
    assertTrue(list.contains(VersionedChildEntity.class));
    assertTrue(list.contains(AbstractVersionedBase.class));
}
Also used : VersionedChildEntity(org.mongodb.morphia.entities.version.VersionedChildEntity) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappedClass(org.mongodb.morphia.mapping.MappedClass) AbstractVersionedBase(org.mongodb.morphia.entities.version.AbstractVersionedBase) Test(org.junit.Test)

Example 74 with MappedClass

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

the class TestVersionAnnotation method testCanMapAnEntityWithAnAbstractVersionedParent.

@Test
public void testCanMapAnEntityWithAnAbstractVersionedParent() {
    Morphia morphia = getMorphia().map(VersionedChildEntity.class);
    Collection<MappedClass> mappedClasses = morphia.getMapper().getMappedClasses();
    assertThat(mappedClasses.size(), is(2));
    List<Class<?>> list = new ArrayList<Class<?>>();
    for (MappedClass mappedClass : mappedClasses) {
        list.add(mappedClass.getClazz());
    }
    assertTrue(list.contains(VersionedChildEntity.class));
    assertTrue(list.contains(AbstractVersionedBase.class));
}
Also used : VersionedChildEntity(org.mongodb.morphia.entities.version.VersionedChildEntity) ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) MappedClass(org.mongodb.morphia.mapping.MappedClass) AbstractVersionedBase(org.mongodb.morphia.entities.version.AbstractVersionedBase) Test(org.junit.Test)

Example 75 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass 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)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2