Search in sources :

Example 91 with EntityModel

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

the class EntityModelTest method testInheritedTypes.

@Test
public void testInheritedTypes() {
    EntityModel model = getDs().getMapper().map(MoreSpecificEntity.class).get(0);
    MoreSpecificEntity beforeDB = new MoreSpecificEntity();
    beforeDB.setId(UUID.randomUUID());
    beforeDB.setTest("a string");
    beforeDB.setTest2(UUID.randomUUID());
    beforeDB.setNumber(13);
    beforeDB.setNumber2(14);
    getDs().save(beforeDB);
    assertEquals(model.getProperty("id").getType(), UUID.class);
    assertEquals(model.getProperty("test").getType(), String.class);
    assertEquals(model.getProperty("test2").getType(), UUID.class);
    getDs().getDatabase().getCollection("specificEntity").deleteMany(new Document());
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 92 with EntityModel

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

the class Operations method versionUpdate.

protected void versionUpdate() {
    PropertyModel versionField = entityModel.getVersionProperty();
    if (versionField != null) {
        List<OperationTarget> operationTargets = ops.get("$inc");
        String version = versionField.getMappedName();
        boolean already = operationTargets != null && operationTargets.stream().anyMatch(tv -> {
            PathTarget target = tv.getTarget();
            return target != null && target.translatedPath().equals(version);
        });
        if (!already) {
            add("$inc", new OperationTarget(new PathTarget(datastore.getMapper(), entityModel, versionField.getName()), 1L));
        }
    }
}
Also used : Document(org.bson.Document) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) List(java.util.List) PathTarget(dev.morphia.internal.PathTarget) Map(java.util.Map) StringJoiner(java.util.StringJoiner) Entry(java.util.Map.Entry) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) HashMap(java.util.HashMap) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) PathTarget(dev.morphia.internal.PathTarget) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel)

Example 93 with EntityModel

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

the class Projection method project.

private Document project(Mapper mapper, Class<?> clazz) {
    Document projection = new Document();
    iterate(mapper, projection, clazz, includes, 1);
    iterate(mapper, projection, clazz, excludes, 0);
    final EntityModel model = mapper.getEntityModel(clazz);
    Entity entityAnnotation = model.getEntityAnnotation();
    if (isIncluding() && entityAnnotation != null && entityAnnotation.useDiscriminator()) {
        projection.put(mapper.getOptions().getDiscriminatorKey(), 1);
    }
    return projection;
}
Also used : Entity(dev.morphia.annotations.Entity) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Document(org.bson.Document)

Example 94 with EntityModel

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

the class SingleReference method decode.

/**
 * Decodes a document in to an entity
 *
 * @param datastore   the datastore
 * @param mapper      the mapper
 * @param mappedField the MappedField
 * @param paramType   the type of the underlying entity
 * @param document    the Document to decode
 * @return the entity
 */
public static MorphiaReference<?> decode(Datastore datastore, Mapper mapper, PropertyModel mappedField, Class<?> paramType, Document document) {
    final EntityModel entityModel = mapper.getEntityModel(paramType);
    Object id = document.get(mappedField.getMappedName());
    return new SingleReference<>(datastore, mapper, entityModel, id);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel)

Aggregations

EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)94 Test (org.testng.annotations.Test)56 Document (org.bson.Document)36 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 ArrayList (java.util.ArrayList)16 Datastore (dev.morphia.Datastore)14 Entity (dev.morphia.annotations.Entity)14 PathTarget (dev.morphia.internal.PathTarget)12 Mapper (dev.morphia.mapping.Mapper)12 Nullable (com.mongodb.lang.Nullable)10 Index (dev.morphia.annotations.Index)10 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)8 VersionedChildEntity (dev.morphia.test.models.versioned.VersionedChildEntity)8 List (java.util.List)8 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)6 WriteConcern (com.mongodb.WriteConcern)4 Key (dev.morphia.Key)4 IndexOptions (dev.morphia.annotations.IndexOptions)4 MappingException (dev.morphia.mapping.MappingException)4 EntityModelBuilder (dev.morphia.mapping.codec.pojo.EntityModelBuilder)4