Search in sources :

Example 6 with MapperOptions

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

the class MorphiaDefaultsConvention method apply.

@Override
public void apply(Mapper mapper, EntityModelBuilder modelBuilder) {
    MapperOptions options = mapper.getOptions();
    final Entity entity = modelBuilder.getAnnotation(Entity.class);
    final Embedded embedded = modelBuilder.getAnnotation(Embedded.class);
    final ExternalEntity externalEntity = modelBuilder.getAnnotation(ExternalEntity.class);
    if (entity != null) {
        modelBuilder.enableDiscriminator(entity.useDiscriminator());
        modelBuilder.discriminatorKey(applyDefaults(entity.discriminatorKey(), options.getDiscriminatorKey()));
    } else if (externalEntity != null) {
        modelBuilder.enableDiscriminator(externalEntity.useDiscriminator());
        modelBuilder.discriminatorKey(applyDefaults(externalEntity.discriminatorKey(), options.getDiscriminatorKey()));
        modelBuilder.annotation(EntityBuilder.entityBuilder().cap(externalEntity.cap()).concern(externalEntity.concern()).discriminator(externalEntity.discriminator()).discriminatorKey(externalEntity.discriminatorKey()).value(externalEntity.value()).useDiscriminator(externalEntity.useDiscriminator()).build());
        modelBuilder.targetType(externalEntity.target());
    } else {
        modelBuilder.enableDiscriminator(embedded == null || embedded.useDiscriminator());
        modelBuilder.discriminatorKey(applyDefaults(embedded != null ? embedded.discriminatorKey() : Mapper.IGNORED_FIELDNAME, options.getDiscriminatorKey()));
    }
    options.getDiscriminator().apply(modelBuilder);
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) ExternalEntity(dev.morphia.annotations.experimental.ExternalEntity) Entity(dev.morphia.annotations.Entity) ExternalEntity(dev.morphia.annotations.experimental.ExternalEntity) Embedded(dev.morphia.annotations.Embedded)

Example 7 with MapperOptions

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

the class TestBase method withOptions.

protected void withOptions(MapperOptions options, Runnable block) {
    MapperOptions previousOptions = mapperOptions;
    try {
        mapperOptions = options;
        database = null;
        datastore = null;
        block.run();
    } finally {
        mapperOptions = previousOptions;
        database = null;
        datastore = null;
    }
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions)

Example 8 with MapperOptions

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

the class TestMapping method collectionNaming.

@Test
public void collectionNaming() {
    MapperOptions options = MapperOptions.builder().collectionNaming(NamingStrategy.lowerCase()).build();
    Datastore datastore = createDatastore(TestBase.TEST_DB_NAME, options);
    List<EntityModel> map = datastore.getMapper().map(ContainsMapWithEmbeddedInterface.class, ContainsIntegerList.class);
    assertEquals(map.get(0).getCollectionName(), "containsmapwithembeddedinterface");
    assertEquals(map.get(1).getCollectionName(), "cil");
    options = MapperOptions.builder().collectionNaming(NamingStrategy.kebabCase()).build();
    datastore = createDatastore(TestBase.TEST_DB_NAME, options);
    map = datastore.getMapper().map(ContainsMapWithEmbeddedInterface.class, ContainsIntegerList.class);
    assertEquals(map.get(0).getCollectionName(), "contains-map-with-embedded-interface");
    assertEquals(map.get(1).getCollectionName(), "cil");
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

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