mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-29 23:32:12 +00:00
Enhancement for random Id of elasticsearch if an entity does not have one
This commit is contained in:
parent
e54b263be9
commit
c689470cac
@ -261,8 +261,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
String type = isBlank(query.getType())?
|
String type = isBlank(query.getType())?
|
||||||
retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType();
|
retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType();
|
||||||
|
|
||||||
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName,type,query.getId())
|
IndexRequestBuilder indexRequestBuilder = null;
|
||||||
.setSource(objectMapper.writeValueAsString(query.getObject()));
|
|
||||||
|
if(query.getId() != null){
|
||||||
|
indexRequestBuilder = client.prepareIndex(indexName,type,query.getId());
|
||||||
|
}else {
|
||||||
|
indexRequestBuilder = client.prepareIndex(indexName,type);
|
||||||
|
}
|
||||||
|
|
||||||
|
indexRequestBuilder.setSource(objectMapper.writeValueAsString(query.getObject()));
|
||||||
|
|
||||||
if(query.getVersion() != null){
|
if(query.getVersion() != null){
|
||||||
indexRequestBuilder.setVersion(query.getVersion());
|
indexRequestBuilder.setVersion(query.getVersion());
|
||||||
indexRequestBuilder.setVersionType(EXTERNAL);
|
indexRequestBuilder.setVersionType(EXTERNAL);
|
||||||
|
@ -55,10 +55,13 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
|||||||
public ID getId(T entity) {
|
public ID getId(T entity) {
|
||||||
ElasticsearchPersistentProperty id = entityMetadata.getIdProperty();
|
ElasticsearchPersistentProperty id = entityMetadata.getIdProperty();
|
||||||
try {
|
try {
|
||||||
|
if(id != null){
|
||||||
return (ID) BeanWrapper.create(entity, null).getProperty(id);
|
return (ID) BeanWrapper.create(entity, null).getProperty(id);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("ID could not be resolved", e);
|
throw new IllegalStateException("ID could not be resolved", e);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user