use of org.springframework.data.projection.EntityProjection in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method projectShouldReadNestedProjection.
// GH-2860
@Test
void projectShouldReadNestedProjection() {
org.bson.Document source = new org.bson.Document("addresses", Collections.singletonList(new org.bson.Document("s", "hwy")));
EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
EntityProjection<WithNestedProjection, Person> projection = introspector.introspect(WithNestedProjection.class, Person.class);
WithNestedProjection person = converter.project(projection, source);
assertThat(person.getAddresses()).extracting(AddressProjection::getStreet).hasSize(1).containsOnly("hwy");
}
use of org.springframework.data.projection.EntityProjection in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method projectShouldReadSimpleDtoProjection.
// GH-2860
@Test
void projectShouldReadSimpleDtoProjection() {
org.bson.Document source = new org.bson.Document("birthDate", new LocalDate(1999, 12, 1).toDate()).append("foo", "Walter");
EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
EntityProjection<PersonDto, Person> projection = introspector.introspect(PersonDto.class, Person.class);
PersonDto person = converter.project(projection, source);
assertThat(person.getBirthDate()).isEqualTo(new LocalDate(1999, 12, 1));
assertThat(person.getFirstname()).isEqualTo("Walter");
}
use of org.springframework.data.projection.EntityProjection in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method projectShouldReadProjectionWithNestedEntity.
// GH-2860
@Test
void projectShouldReadProjectionWithNestedEntity() {
org.bson.Document source = new org.bson.Document("addresses", Collections.singletonList(new org.bson.Document("s", "hwy")));
EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
EntityProjection<ProjectionWithNestedEntity, Person> projection = introspector.introspect(ProjectionWithNestedEntity.class, Person.class);
ProjectionWithNestedEntity person = converter.project(projection, source);
assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy");
}
use of org.springframework.data.projection.EntityProjection in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method findAndReplace.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findAndReplace(org.springframework.data.mongodb.core.query.Query, java.lang.Object, org.springframework.data.mongodb.core.FindAndReplaceOptions, java.lang.Class, java.lang.String, java.lang.Class)
*/
@Override
public <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType, String collectionName, Class<T> resultType) {
Assert.notNull(query, "Query must not be null!");
Assert.notNull(replacement, "Replacement must not be null!");
Assert.notNull(options, "Options must not be null! Use FindAndReplaceOptions#empty() instead.");
Assert.notNull(entityType, "Entity class must not be null!");
Assert.notNull(collectionName, "CollectionName must not be null!");
Assert.notNull(resultType, "ResultType must not be null! Use Object.class instead.");
Assert.isTrue(query.getLimit() <= 1, "Query must not define a limit other than 1 ore none!");
Assert.isTrue(query.getSkip() <= 0, "Query must not define skip.");
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityType);
QueryContext queryContext = queryOperations.createQueryContext(query);
EntityProjection<T, S> projection = operations.introspectProjection(resultType, entityType);
Document mappedQuery = queryContext.getMappedQuery(entity);
Document mappedFields = queryContext.getMappedFields(entity, projection);
Document mappedSort = queryContext.getMappedSort(entity);
return Mono.defer(() -> {
PersistableEntityModel<S> pem = PersistableEntityModel.of(replacement, collectionName);
maybeEmitEvent(new BeforeConvertEvent<>(pem.getSource(), pem.getCollection()));
return maybeCallBeforeConvert(pem.getSource(), pem.getCollection()).map(pem::mutate).flatMap(it -> {
PersistableEntityModel<S> mapped = it.addTargetDocument(operations.forEntity(it.getSource()).toMappedDocument(mongoConverter).getDocument());
maybeEmitEvent(new BeforeSaveEvent(mapped.getSource(), mapped.getTarget(), mapped.getCollection()));
return maybeCallBeforeSave(it.getSource(), mapped.getTarget(), mapped.getCollection()).map(potentiallyModified -> PersistableEntityModel.of(potentiallyModified, mapped.getTarget(), mapped.getCollection()));
}).flatMap(it -> {
Mono<T> afterFindAndReplace = doFindAndReplace(it.getCollection(), mappedQuery, mappedFields, mappedSort, queryContext.getCollation(entityType).orElse(null), entityType, it.getTarget(), options, projection);
return afterFindAndReplace.flatMap(saved -> {
maybeEmitEvent(new AfterSaveEvent<>(saved, it.getTarget(), it.getCollection()));
return maybeCallAfterSave(saved, it.getTarget(), it.getCollection());
});
});
});
}
use of org.springframework.data.projection.EntityProjection in project spring-data-mongodb by spring-projects.
the class MappingMongoConverterUnitTests method projectShouldReadSimpleInterfaceProjection.
// GH-2860
@Test
void projectShouldReadSimpleInterfaceProjection() {
org.bson.Document source = new org.bson.Document("birthDate", new LocalDate(1999, 12, 1).toDate()).append("foo", "Walter");
EntityProjectionIntrospector discoverer = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
EntityProjection<PersonProjection, Person> projection = discoverer.introspect(PersonProjection.class, Person.class);
PersonProjection person = converter.project(projection, source);
assertThat(person.getBirthDate()).isEqualTo(new LocalDate(1999, 12, 1));
assertThat(person.getFirstname()).isEqualTo("Walter");
}
Aggregations