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