Search in sources :

Example 1 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by mongodb.

the class AggregationImpl method execute.

@Override
public <R> MorphiaCursor<R> execute(Class<R> resultType) {
    MongoCursor<R> cursor;
    if (datastore.getMapper().isMappable(resultType) && !resultType.equals(this.collection.getDocumentClass())) {
        MongoCollection<Document> collection = this.collection.withDocumentClass(Document.class);
        MongoCursor<Document> results = collection.aggregate(getDocuments()).iterator();
        EntityModel entityModel = datastore.getMapper().getEntityModel(this.collection.getDocumentClass());
        cursor = new MappingCursor<>(results, datastore.getCodecRegistry().get(resultType), entityModel.getDiscriminatorKey());
    } else {
        cursor = collection.aggregate(getDocuments(), resultType).iterator();
    }
    return new MorphiaCursor<>(cursor);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Document(org.bson.Document) MorphiaCursor(dev.morphia.query.internal.MorphiaCursor)

Example 2 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by mongodb.

the class TestDocumentValidation method validationDocuments.

@Test
public void validationDocuments() {
    Document validator = parse("{ \"jelly\" : { \"$ne\" : \"rhubarb\" } }");
    getMapper().map(DocumentValidation.class);
    EntityModel model = getMapper().getEntityModel(DocumentValidation.class);
    for (ValidationLevel level : EnumSet.allOf(ValidationLevel.class)) {
        for (ValidationAction action : EnumSet.allOf(ValidationAction.class)) {
            checkValidation(validator, model, level, action);
        }
    }
}
Also used : ValidationAction(com.mongodb.client.model.ValidationAction) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ValidationLevel(com.mongodb.client.model.ValidationLevel) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 3 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by mongodb.

the class TestMapping method subTypes.

@Test
public void subTypes() {
    getMapper().map(EmbeddedType.class, Another.class, Child.class);
    Mapper mapper = getMapper();
    List<EntityModel> subTypes = mapper.getEntityModel(EmbeddedType.class).getSubtypes();
    Assert.assertTrue(subTypes.contains(mapper.getEntityModel(Another.class)));
    Assert.assertTrue(subTypes.contains(mapper.getEntityModel(Child.class)));
}
Also used : Mapper(dev.morphia.mapping.Mapper) EmbeddedType(dev.morphia.test.models.generics.EmbeddedType) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 4 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by mongodb.

the class TestMapping method testMethodMapping.

@Test
public void testMethodMapping() {
    Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
    EntityModel model = datastore.getMapper().map(MethodMappedUser.class).get(0);
    assertTrue(model.getProperties().size() > 0);
    assertNotNull(model.getVersionProperty(), model.getProperties().toString());
    assertNotNull(model.getProperty("dateJoined"));
    assertNotNull(model.getProperty("joined"));
    assertNotNull(model.getProperty("friend_reference"));
    assertNotNull(model.getProperty("morphia_reference"));
}
Also used : MethodMappedUser(dev.morphia.test.models.methods.MethodMappedUser) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 5 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by mongodb.

the class TestVersioning method testCanMapAnEntityWithAnAbstractVersionedParent.

@Test
public void testCanMapAnEntityWithAnAbstractVersionedParent() {
    Datastore datastore = Morphia.createDatastore(getMongoClient(), TEST_DB_NAME);
    Mapper mapper = datastore.getMapper();
    mapper.map(VersionedChildEntity.class);
    List<EntityModel> mappedEntities = mapper.getMappedEntities();
    assertEquals(mappedEntities.size(), 2, mappedEntities.toString());
    List<Class<?>> list = new ArrayList<>();
    for (EntityModel entityModel : mappedEntities) {
        list.add(entityModel.getType());
    }
    assertTrue(list.contains(VersionedChildEntity.class));
    assertTrue(list.contains(AbstractVersionedBase.class));
}
Also used : Mapper(dev.morphia.mapping.Mapper) VersionedChildEntity(dev.morphia.test.models.versioned.VersionedChildEntity) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ArrayList(java.util.ArrayList) AbstractVersionedBase(dev.morphia.test.models.versioned.AbstractVersionedBase) Test(org.testng.annotations.Test)

Aggregations

EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)47 Test (org.testng.annotations.Test)28 Document (org.bson.Document)18 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)11 ArrayList (java.util.ArrayList)8 Datastore (dev.morphia.Datastore)7 Entity (dev.morphia.annotations.Entity)7 PathTarget (dev.morphia.internal.PathTarget)6 Mapper (dev.morphia.mapping.Mapper)6 Nullable (com.mongodb.lang.Nullable)5 Index (dev.morphia.annotations.Index)5 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)4 VersionedChildEntity (dev.morphia.test.models.versioned.VersionedChildEntity)4 List (java.util.List)4 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)3 WriteConcern (com.mongodb.WriteConcern)2 Key (dev.morphia.Key)2 IndexOptions (dev.morphia.annotations.IndexOptions)2 MappingException (dev.morphia.mapping.MappingException)2 EntityModelBuilder (dev.morphia.mapping.codec.pojo.EntityModelBuilder)2