From 4fb2b33066149d4189746ca1c06782c41d67d628 Mon Sep 17 00:00:00 2001 From: Young Gu Date: Thu, 18 Aug 2016 11:49:56 +0800 Subject: [PATCH] DATAES-281 - Setting identifier values now uses PersistentPropertyAccessor API. DefaultResultMapper and ElasticsearchTemplate now use the PersistentPropertyAccessor API to set identifier values on objects instead of using reflection. Original pull request: #156. --- .../core/DefaultResultMapper.java | 12 +-- .../core/ElasticsearchTemplate.java | 12 +-- .../ImmutableElasticsearchRepository.java | 28 +++++++ ...ImmutableElasticsearchRepositoryTests.java | 79 +++++++++++++++++++ .../immutable/ImmutableEntity.java | 55 +++++++++++++ .../resources/immutable-repository-test.xml | 19 +++++ 6 files changed, 187 insertions(+), 18 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepository.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepositoryTests.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableEntity.java create mode 100644 src/test/resources/immutable-repository-test.xml diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java index 0add0f2c9..5d28b3bac 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java @@ -170,17 +170,11 @@ public class DefaultResultMapper extends AbstractResultMapper { private void setPersistentEntityId(T result, String id, Class clazz) { if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { - PersistentProperty idProperty = mappingContext.getPersistentEntity(clazz).getIdProperty(); + ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz); + PersistentProperty idProperty = persistentEntity.getIdProperty(); // Only deal with String because ES generated Ids are strings ! if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { - Method setter = idProperty.getSetter(); - if (setter != null) { - try { - setter.invoke(result, id); - } catch (Throwable t) { - t.printStackTrace(); - } - } + persistentEntity.getPropertyAccessor(result).setProperty(idProperty,id); } } } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java index 9bb081e16..76bcbc2e7 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java @@ -1099,17 +1099,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati } private void setPersistentEntityId(Object entity, String id) { - PersistentProperty idProperty = getPersistentEntityFor(entity.getClass()).getIdProperty(); + ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(entity.getClass()); + PersistentProperty idProperty = persistentEntity.getIdProperty(); // Only deal with String because ES generated Ids are strings ! if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { - Method setter = idProperty.getSetter(); - if (setter != null) { - try { - setter.invoke(entity, id); - } catch (Throwable t) { - t.printStackTrace(); - } - } + persistentEntity.getPropertyAccessor(entity).setProperty(idProperty,id); } } diff --git a/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepository.java b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepository.java new file mode 100644 index 000000000..e3885c60b --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepository.java @@ -0,0 +1,28 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.immutable; + +import org.springframework.data.elasticsearch.entities.SampleEntity; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; + +import java.util.List; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + */ +public interface ImmutableElasticsearchRepository extends ElasticsearchRepository { +} diff --git a/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepositoryTests.java b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepositoryTests.java new file mode 100644 index 000000000..24f4a41cd --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepositoryTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.immutable; + +import com.google.common.collect.Lists; +import org.hamcrest.core.IsEqual; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.data.elasticsearch.entities.SampleEntity; +import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.apache.commons.lang.RandomStringUtils.randomNumeric; +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:/immutable-repository-test.xml") +public class ImmutableElasticsearchRepositoryTests { + + @Autowired + private ImmutableElasticsearchRepository repository; + + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; + + + @Before + public void before() { + elasticsearchTemplate.deleteIndex(ImmutableEntity.class); + elasticsearchTemplate.createIndex(ImmutableEntity.class); + elasticsearchTemplate.refresh(ImmutableEntity.class); + } + @Test + public void shouldSaveAndFindImmutableDocument() { + // when + ImmutableEntity entity = repository.save(new ImmutableEntity("test name")); + assertThat(entity.getId(), is(notNullValue())); + // then + ImmutableEntity entityFromElasticSearch = repository.findOne(entity.getId()); + assertThat(entityFromElasticSearch.getName(), new IsEqual("test name")); + assertThat(entityFromElasticSearch.getId(), new IsEqual(entity.getId())); + + } + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableEntity.java b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableEntity.java new file mode 100644 index 000000000..cc34650ff --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableEntity.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.immutable; + +import lombok.*; +import org.springframework.data.annotation.AccessType; +import org.springframework.data.annotation.Id; +import org.springframework.data.annotation.Version; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; +import org.springframework.data.elasticsearch.annotations.ScriptedField; +import org.springframework.data.elasticsearch.core.geo.GeoPoint; + +/** + * @author Rizwan Idrees + * @author Mohsin Husen + */ +@AccessType(AccessType.Type.FIELD) +@Document(indexName = "test-index", type = "immutable-entity", shards = 1, replicas = 0, refreshInterval = "-1") +public class ImmutableEntity { + + @Id + private String id; + private String name; + + private ImmutableEntity() { + } + + public ImmutableEntity(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + +} diff --git a/src/test/resources/immutable-repository-test.xml b/src/test/resources/immutable-repository-test.xml new file mode 100644 index 000000000..1d57ab642 --- /dev/null +++ b/src/test/resources/immutable-repository-test.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + \ No newline at end of file