Don't update indexed object if it is no persistent entity.

Original Pull Request  #1673
Closes #803
This commit is contained in:
Peter-Josef Meisch 2021-01-30 14:16:22 +01:00 committed by GitHub
parent dbd99a3880
commit ddc7246c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 20 deletions

View File

@ -66,7 +66,6 @@ import org.springframework.data.elasticsearch.support.VersionInfo;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.util.CloseableIterator;
import org.springframework.data.util.Streamable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
@ -95,7 +94,8 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
Assert.notNull(elasticsearchConverter, "elasticsearchConverter must not be null.");
this.elasticsearchConverter = elasticsearchConverter;
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = this.elasticsearchConverter.getMappingContext();
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext = this.elasticsearchConverter
.getMappingContext();
this.entityOperations = new EntityOperations(mappingContext);
this.routingResolver = new DefaultRoutingResolver((SimpleElasticsearchMappingContext) mappingContext);
@ -611,7 +611,11 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
}
protected void updateIndexedObject(Object entity, IndexedObjectInformation indexedObjectInformation) {
ElasticsearchPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
.getPersistentEntity(entity.getClass());
if (persistentEntity != null) {
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
@ -632,6 +636,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
}
}
}
ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) {
return elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz);

View File

@ -148,7 +148,6 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
IndexRequest request = prepareWriteRequest(requestFactory.indexRequest(query, index));
IndexResponse indexResponse = execute(client -> client.index(request, RequestOptions.DEFAULT));
// We should call this because we are not going through a mapper.
Object queryObject = query.getObject();
if (queryObject != null) {
updateIndexedObject(queryObject, IndexedObjectInformation.of(indexResponse.getId(), indexResponse.getSeqNo(),

View File

@ -168,7 +168,6 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
}
String documentId = response.getId();
// We should call this because we are not going through a mapper.
Object queryObject = query.getObject();
if (queryObject != null) {
updateIndexedObject(queryObject, IndexedObjectInformation.of(documentId, response.getSeqNo(),