use of dev.morphia.annotations.Entity 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);
}
use of dev.morphia.annotations.Entity in project morphia by mongodb.
the class TestMapping method testExternalClass.
@Test
public void testExternalClass() {
assertFalse(getDs().getMapper().isMapped(HoldsUnannotated.class));
assertFalse(getDs().getMapper().isMapped(ThirdPartyEmbedded.class));
assertFalse(getDs().getMapper().isMapped(ThirdPartyEntity.class));
assertThrows(MappingException.class, () -> getDs().getMapper().map(ThirdPartyEntity.class));
assertThrows(MappingException.class, () -> getDs().getMapper().map(ThirdPartyEmbedded.class));
getDs().getMapper().mapPackageFromClass(HoldsUnannotated.class);
assertTrue(getDs().getMapper().isMapped(ThirdPartyEmbedded.class));
assertTrue(getDs().getMapper().isMapped(ThirdPartyEntity.class));
HoldsUnannotated holdsUnannotated = new HoldsUnannotated();
holdsUnannotated.embedded = new ThirdPartyEmbedded();
holdsUnannotated.embedded.number = 42L;
holdsUnannotated.embedded.field = "Left";
getDs().save(holdsUnannotated);
assertEquals(getDs().find(HoldsUnannotated.class).first(), holdsUnannotated);
withOptions(MapperOptions.DEFAULT, () -> {
assertFalse(getDs().getMapper().map(ThirdPartyEntityProxy.class).isEmpty());
assertFalse(getDs().getMapper().map(ThirdPartyEmbeddedProxy.class).isEmpty());
EntityModel model = getDs().getMapper().getEntityModel(ThirdPartyEntity.class);
assertEquals(model.getCollectionName(), "extEnt");
assertEquals(model.getDiscriminator(), "ext");
assertEquals(model.getDiscriminatorKey(), "_xt");
Entity annotation = model.getAnnotation(Entity.class);
assertEquals(annotation.concern(), "JOURNALED");
assertEquals(annotation.cap().count(), 123);
assertEquals(annotation.cap().value(), 456);
ThirdPartyEntity entity = new ThirdPartyEntity();
entity.field = "hi";
entity.number = 42L;
getDs().save(entity);
assertEquals(getDs().find(ThirdPartyEntity.class).first(), entity);
});
}
use of dev.morphia.annotations.Entity in project morphia by mongodb.
the class TestMapping method testExternalClassUsingMethods.
@Test
public void testExternalClassUsingMethods() {
withOptions(MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build(), () -> {
assertFalse(getDs().getMapper().map(ThirdPartyEntityProxy.class).isEmpty());
assertFalse(getDs().getMapper().map(ThirdPartyEmbeddedProxy.class).isEmpty());
EntityModel model = getDs().getMapper().getEntityModel(ThirdPartyEntity.class);
assertEquals(model.getCollectionName(), "extEnt");
assertEquals(model.getDiscriminator(), "ext");
assertEquals(model.getDiscriminatorKey(), "_xt");
Entity annotation = model.getAnnotation(Entity.class);
assertEquals(annotation.concern(), "JOURNALED");
assertEquals(annotation.cap().count(), 123);
assertEquals(annotation.cap().value(), 456);
ThirdPartyEntity entity = new ThirdPartyEntity();
entity.setField("hi");
entity.setNumber(42L);
getDs().save(entity);
assertEquals(getDs().find(ThirdPartyEntity.class).first(), entity);
});
}
Aggregations