Search in sources :

Example 1 with MapperOptions

use of dev.morphia.mapping.MapperOptions in project morphia by mongodb.

the class TestEnumMapping method testCustomerWithArrayList.

@Test
public void testCustomerWithArrayList() {
    MapperOptions options = MapperOptions.builder(getMapper().getOptions()).storeEmpties(true).storeNulls(true).build();
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), options);
    Mapper mapper = datastore.getMapper();
    mapper.map(CustomerWithArrayList.class);
    CustomerWithArrayList customer = new CustomerWithArrayList();
    List<WebTemplate> templates1 = new ArrayList<>();
    templates1.add(new WebTemplate("template #1.1"));
    templates1.add(new WebTemplate("template #1.2"));
    customer.add(WebTemplateType.CrewContract, templates1);
    List<WebTemplate> templates2 = new ArrayList<>();
    templates1.add(new WebTemplate("template #2.1"));
    templates1.add(new WebTemplate("template #2.2"));
    customer.add(WebTemplateType.CrewContractHeader, templates2);
    getDs().save(customer);
    final Datastore datastore1 = getDs();
    CustomerWithArrayList loaded = datastore1.find(CustomerWithArrayList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithArrayList, loaded.mapWithArrayList);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Mapper(dev.morphia.mapping.Mapper) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 2 with MapperOptions

use of dev.morphia.mapping.MapperOptions in project morphia by mongodb.

the class TestClassLoader method recreateCollection.

private MongoCollection<BasicEntity> recreateCollection(ClassLoader classLoader) {
    MapperOptions options = MapperOptions.builder().discriminator(DiscriminatorFunction.className()).classLoader(classLoader).build();
    Datastore datastore = Morphia.createDatastore(getMongoClient(), TEST_DB_NAME, options);
    return datastore.getCollection(BasicEntity.class);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Datastore(dev.morphia.Datastore)

Example 3 with MapperOptions

use of dev.morphia.mapping.MapperOptions in project morphia by mongodb.

the class TestEnumMapping method testCustomerWithList.

@Test
public void testCustomerWithList() {
    MapperOptions options = MapperOptions.builder(getMapper().getOptions()).storeEmpties(true).storeNulls(true).build();
    final Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), options);
    Mapper mapper = datastore.getMapper();
    mapper.map(CustomerWithArrayList.class);
    CustomerWithList customer = new CustomerWithList();
    List<WebTemplate> templates1 = new ArrayList<>();
    templates1.add(new WebTemplate("template #1.1"));
    templates1.add(new WebTemplate("template #1.2"));
    customer.add(WebTemplateType.CrewContract, templates1);
    List<WebTemplate> templates2 = new ArrayList<>();
    templates1.add(new WebTemplate("template #2.1"));
    templates1.add(new WebTemplate("template #2.2"));
    customer.add(WebTemplateType.CrewContractHeader, templates2);
    getDs().save(customer);
    final Datastore datastore1 = getDs();
    CustomerWithList loaded = datastore1.find(CustomerWithList.class).filter(eq("_id", customer.id)).first();
    assertEquals(customer.mapWithList, loaded.mapWithList);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Mapper(dev.morphia.mapping.Mapper) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 4 with MapperOptions

use of dev.morphia.mapping.MapperOptions in project morphia by mongodb.

the class PropertyModelBuilder method discoverMappedName.

public PropertyModelBuilder discoverMappedName() {
    MapperOptions options = mapper.getOptions();
    Property property = getAnnotation(Property.class);
    Reference reference = getAnnotation(Reference.class);
    Version version = getAnnotation(Version.class);
    if (hasAnnotation(Id.class)) {
        mappedName("_id");
    } else if (property != null && !property.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(property.value());
    } else if (reference != null && !reference.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(reference.value());
    } else if (version != null && !version.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(version.value());
    } else {
        mappedName(options.getFieldNaming().apply(name()));
    }
    return this;
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Version(dev.morphia.annotations.Version) Reference(dev.morphia.annotations.Reference) Property(dev.morphia.annotations.Property)

Example 5 with MapperOptions

use of dev.morphia.mapping.MapperOptions in project morphia by mongodb.

the class ConfigureProperties method apply.

@Override
public void apply(Mapper mapper, EntityModelBuilder modelBuilder) {
    MapperOptions options = mapper.getOptions();
    processProperties(modelBuilder, options);
    if (modelBuilder.idPropertyName() == null) {
        IdField idProperty = modelBuilder.getAnnotation(IdField.class);
        if (idProperty != null) {
            modelBuilder.idPropertyName(idProperty.value());
            PropertyModelBuilder propertyModelBuilder = modelBuilder.propertyModelByName(idProperty.value());
            propertyModelBuilder.mappedName("_id");
        }
    }
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) PropertyModelBuilder(dev.morphia.mapping.codec.pojo.PropertyModelBuilder) IdField(dev.morphia.annotations.experimental.IdField)

Aggregations

MapperOptions (dev.morphia.mapping.MapperOptions)8 Datastore (dev.morphia.Datastore)4 Test (org.testng.annotations.Test)3 Mapper (dev.morphia.mapping.Mapper)2 ArrayList (java.util.ArrayList)2 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Embedded (dev.morphia.annotations.Embedded)1 Entity (dev.morphia.annotations.Entity)1 Property (dev.morphia.annotations.Property)1 Reference (dev.morphia.annotations.Reference)1 Version (dev.morphia.annotations.Version)1 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)1 IdField (dev.morphia.annotations.experimental.IdField)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyModelBuilder (dev.morphia.mapping.codec.pojo.PropertyModelBuilder)1