Search in sources :

Example 1 with Embedded

use of dev.morphia.annotations.Embedded 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 2 with Embedded

use of dev.morphia.annotations.Embedded in project morphia by mongodb.

the class DiscriminatorFunction method apply.

/**
 * Applies the function to the given model to determine the discriminator value
 *
 * @param builder the builder to evaluate
 */
public final void apply(EntityModelBuilder builder) {
    String discriminator = Mapper.IGNORED_FIELDNAME;
    Entity entity = builder.getAnnotation(Entity.class);
    if (entity != null) {
        discriminator = entity.discriminator();
    } else {
        Embedded embedded = builder.getAnnotation(Embedded.class);
        if (embedded != null) {
            discriminator = embedded.discriminator();
        }
    }
    if (discriminator.equals(Mapper.IGNORED_FIELDNAME)) {
        discriminator = compute(builder);
    }
    builder.discriminator(discriminator);
}
Also used : Entity(dev.morphia.annotations.Entity) Embedded(dev.morphia.annotations.Embedded)

Aggregations

Embedded (dev.morphia.annotations.Embedded)2 Entity (dev.morphia.annotations.Entity)2 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)1 MapperOptions (dev.morphia.mapping.MapperOptions)1