Skip to content

Commit 7e736a6

Browse files
hyysguyangodrotbohm
authored andcommitted
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.
1 parent 20c5d19 commit 7e736a6

File tree

6 files changed

+187
-18
lines changed

6 files changed

+187
-18
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,11 @@ public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz)
177177

178178
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
179179
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
180-
PersistentProperty<ElasticsearchPersistentProperty> idProperty = mappingContext.getPersistentEntity(clazz).getIdProperty();
180+
ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz);
181+
PersistentProperty idProperty = persistentEntity.getIdProperty();
181182
// Only deal with String because ES generated Ids are strings !
182183
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
183-
Method setter = idProperty.getSetter();
184-
if (setter != null) {
185-
try {
186-
setter.invoke(result, id);
187-
} catch (Throwable t) {
188-
LOG.error("Unable to set entity Id", t);
189-
}
190-
}
184+
persistentEntity.getPropertyAccessor(result).setProperty(idProperty,id);
191185
}
192186
}
193187
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,17 +1113,11 @@ private String getPersistentEntityId(Object entity) {
11131113
}
11141114

11151115
private void setPersistentEntityId(Object entity, String id) {
1116-
PersistentProperty idProperty = getPersistentEntityFor(entity.getClass()).getIdProperty();
1116+
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(entity.getClass());
1117+
PersistentProperty idProperty = persistentEntity.getIdProperty();
11171118
// Only deal with String because ES generated Ids are strings !
11181119
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
1119-
Method setter = idProperty.getSetter();
1120-
if (setter != null) {
1121-
try {
1122-
setter.invoke(entity, id);
1123-
} catch (Throwable t) {
1124-
t.printStackTrace();
1125-
}
1126-
}
1120+
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty,id);
11271121
}
11281122
}
11291123

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.immutable;
17+
18+
import org.springframework.data.elasticsearch.entities.SampleEntity;
19+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
20+
21+
import java.util.List;
22+
23+
/**
24+
* @author Rizwan Idrees
25+
* @author Mohsin Husen
26+
*/
27+
public interface ImmutableElasticsearchRepository extends ElasticsearchRepository<ImmutableEntity, String> {
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.immutable;
17+
18+
import com.google.common.collect.Lists;
19+
import org.hamcrest.core.IsEqual;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.data.domain.Page;
25+
import org.springframework.data.domain.PageRequest;
26+
import org.springframework.data.domain.Sort;
27+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
28+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
29+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
30+
import org.springframework.data.elasticsearch.entities.SampleEntity;
31+
import org.springframework.data.elasticsearch.repositories.sample.SampleElasticsearchRepository;
32+
import org.springframework.test.context.ContextConfiguration;
33+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34+
35+
import java.util.ArrayList;
36+
import java.util.Arrays;
37+
import java.util.List;
38+
39+
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
40+
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
41+
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
42+
import static org.hamcrest.Matchers.*;
43+
import static org.junit.Assert.*;
44+
45+
/**
46+
* @author Rizwan Idrees
47+
* @author Mohsin Husen
48+
*/
49+
50+
@RunWith(SpringJUnit4ClassRunner.class)
51+
@ContextConfiguration("classpath:/immutable-repository-test.xml")
52+
public class ImmutableElasticsearchRepositoryTests {
53+
54+
@Autowired
55+
private ImmutableElasticsearchRepository repository;
56+
57+
@Autowired
58+
private ElasticsearchTemplate elasticsearchTemplate;
59+
60+
61+
@Before
62+
public void before() {
63+
elasticsearchTemplate.deleteIndex(ImmutableEntity.class);
64+
elasticsearchTemplate.createIndex(ImmutableEntity.class);
65+
elasticsearchTemplate.refresh(ImmutableEntity.class);
66+
}
67+
@Test
68+
public void shouldSaveAndFindImmutableDocument() {
69+
// when
70+
ImmutableEntity entity = repository.save(new ImmutableEntity("test name"));
71+
assertThat(entity.getId(), is(notNullValue()));
72+
// then
73+
ImmutableEntity entityFromElasticSearch = repository.findOne(entity.getId());
74+
assertThat(entityFromElasticSearch.getName(), new IsEqual("test name"));
75+
assertThat(entityFromElasticSearch.getId(), new IsEqual(entity.getId()));
76+
77+
}
78+
79+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.immutable;
17+
18+
import lombok.*;
19+
import org.springframework.data.annotation.AccessType;
20+
import org.springframework.data.annotation.Id;
21+
import org.springframework.data.annotation.Version;
22+
import org.springframework.data.elasticsearch.annotations.Document;
23+
import org.springframework.data.elasticsearch.annotations.Field;
24+
import org.springframework.data.elasticsearch.annotations.FieldType;
25+
import org.springframework.data.elasticsearch.annotations.ScriptedField;
26+
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
27+
28+
/**
29+
* @author Rizwan Idrees
30+
* @author Mohsin Husen
31+
*/
32+
@AccessType(AccessType.Type.FIELD)
33+
@Document(indexName = "test-index", type = "immutable-entity", shards = 1, replicas = 0, refreshInterval = "-1")
34+
public class ImmutableEntity {
35+
36+
@Id
37+
private String id;
38+
private String name;
39+
40+
private ImmutableEntity() {
41+
}
42+
43+
public ImmutableEntity(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getId() {
48+
return id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7+
8+
<import resource="infrastructure.xml"/>
9+
10+
<bean name="elasticsearchTemplate"
11+
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
12+
<constructor-arg name="client" ref="client"/>
13+
</bean>
14+
15+
16+
<elasticsearch:repositories
17+
base-package="org.springframework.data.elasticsearch.immutable"/>
18+
19+
</beans>

0 commit comments

Comments
 (0)