Skip to content

Commit 416a3f2

Browse files
christophstroblodrotbohm
authored andcommitted
DATAES-352 - Adapt to API changes in repository interfaces.
We now follow a more consistent naming scheme for the methods in repository that are driven by the following guidelines: * Methods referring to an identifier now all end on …ById(…). * Methods taking or returning a collection are named …All(…) Please see DATACMNS-944 for details.
1 parent 939c0f9 commit 416a3f2

19 files changed

+158
-141
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232
import org.springframework.dao.InvalidDataAccessApiUsageException;
33-
import org.springframework.data.domain.*;
33+
import org.springframework.data.domain.Page;
34+
import org.springframework.data.domain.PageImpl;
35+
import org.springframework.data.domain.PageRequest;
36+
import org.springframework.data.domain.Pageable;
37+
import org.springframework.data.domain.Sort;
3438
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
35-
import org.springframework.data.elasticsearch.core.query.*;
39+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
40+
import org.springframework.data.elasticsearch.core.query.GetQuery;
41+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
42+
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
43+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
44+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
3645
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
3746
import org.springframework.util.Assert;
3847

@@ -45,6 +54,7 @@
4554
* @author Ryan Henszey
4655
* @author Kevin Leturc
4756
* @author Mark Paluch
57+
* @author Christoph Strobl
4858
*/
4959
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable>
5060
implements ElasticsearchRepository<T, ID> {
@@ -57,18 +67,18 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
5767
public AbstractElasticsearchRepository() {}
5868

5969
public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
60-
70+
6171
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
62-
72+
6373
this.setElasticsearchOperations(elasticsearchOperations);
6474
}
6575

6676
public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata,
6777
ElasticsearchOperations elasticsearchOperations) {
6878
this(elasticsearchOperations);
69-
79+
7080
Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!");
71-
81+
7282
this.entityInformation = metadata;
7383
setEntityClass(this.entityInformation.getJavaType());
7484
try {
@@ -94,7 +104,7 @@ private boolean createIndexAndMapping() {
94104
}
95105

96106
@Override
97-
public Optional<T> findOne(ID id) {
107+
public Optional<T> findById(ID id) {
98108
GetQuery query = new GetQuery();
99109
query.setId(stringIdRepresentation(id));
100110
return Optional.ofNullable(elasticsearchOperations.queryForObject(query, getEntityClass()));
@@ -104,7 +114,7 @@ public Optional<T> findOne(ID id) {
104114
public Iterable<T> findAll() {
105115
int itemCount = (int) this.count();
106116
if (itemCount == 0) {
107-
return new PageImpl<>(Collections.<T>emptyList());
117+
return new PageImpl<>(Collections.<T> emptyList());
108118
}
109119
return this.findAll(PageRequest.of(0, Math.max(1, itemCount)));
110120
}
@@ -119,15 +129,15 @@ public Page<T> findAll(Pageable pageable) {
119129
public Iterable<T> findAll(Sort sort) {
120130
int itemCount = (int) this.count();
121131
if (itemCount == 0) {
122-
return new PageImpl<>(Collections.<T>emptyList());
132+
return new PageImpl<>(Collections.<T> emptyList());
123133
}
124134
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
125135
.withPageable(PageRequest.of(0, itemCount, sort)).build();
126136
return elasticsearchOperations.queryForPage(query, getEntityClass());
127137
}
128138

129139
@Override
130-
public Iterable<T> findAll(Iterable<ID> ids) {
140+
public Iterable<T> findAllById(Iterable<ID> ids) {
131141
Assert.notNull(ids, "ids can't be null.");
132142
SearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
133143
return elasticsearchOperations.multiGet(query, getEntityClass());
@@ -165,7 +175,7 @@ public <S extends T> S index(S entity) {
165175
}
166176

167177
@Override
168-
public <S extends T> Iterable<S> save(Iterable<S> entities) {
178+
public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
169179
Assert.notNull(entities, "Cannot insert 'null' as a List.");
170180
List<IndexQuery> queries = new ArrayList<>();
171181
for (S s : entities) {
@@ -177,16 +187,16 @@ public <S extends T> Iterable<S> save(Iterable<S> entities) {
177187
}
178188

179189
@Override
180-
public boolean exists(ID id) {
181-
return findOne(id) != null;
190+
public boolean existsById(ID id) {
191+
return findById(id) != null;
182192
}
183193

184194
@Override
185195
public Iterable<T> search(QueryBuilder query) {
186196
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
187197
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
188198
if (count == 0) {
189-
return new PageImpl<>(Collections.<T>emptyList());
199+
return new PageImpl<>(Collections.<T> emptyList());
190200
}
191201
searchQuery.setPageable(PageRequest.of(0, count));
192202
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
@@ -217,7 +227,7 @@ public Page<T> searchSimilar(T entity, String[] fields, Pageable pageable) {
217227
}
218228

219229
@Override
220-
public void delete(ID id) {
230+
public void deleteById(ID id) {
221231
Assert.notNull(id, "Cannot delete entity with id 'null'.");
222232
elasticsearchOperations.delete(entityInformation.getIndexName(), entityInformation.getType(),
223233
stringIdRepresentation(id));
@@ -227,12 +237,12 @@ public void delete(ID id) {
227237
@Override
228238
public void delete(T entity) {
229239
Assert.notNull(entity, "Cannot delete 'null' entity.");
230-
delete(extractIdFromBean(entity));
240+
deleteById(extractIdFromBean(entity));
231241
elasticsearchOperations.refresh(entityInformation.getIndexName());
232242
}
233243

234244
@Override
235-
public void delete(Iterable<? extends T> entities) {
245+
public void deleteAll(Iterable<? extends T> entities) {
236246
Assert.notNull(entities, "Cannot delete 'null' list.");
237247
for (T entity : entities) {
238248
delete(entity);

src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,17 +15,16 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.repository.core.EntityInformation;
2119

2220
/**
2321
* @param <T>
2422
* @param <ID>
2523
* @author Rizwan Idrees
2624
* @author Mohsin Husen
25+
* @author Christoph Strobl
2726
*/
28-
public interface ElasticsearchEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> {
27+
public interface ElasticsearchEntityInformation<T, ID> extends EntityInformation<T, ID> {
2928

3029
String getIdAttribute();
3130

src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,14 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import java.io.Serializable;
19-
2018
/**
2119
* ElasticsearchEntityInformationCreator
2220
*
2321
* @author Rizwan Idrees
2422
* @author Mohsin Husen
23+
* @author Christoph Strobl
2524
*/
2625
public interface ElasticsearchEntityInformationCreator {
2726

28-
<T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass);
27+
<T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass);
2928
}

src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
2119
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
2220
import org.springframework.data.mapping.context.MappingContext;
@@ -29,22 +27,23 @@
2927
* @author Mohsin Husen
3028
* @author Oliver Gierke
3129
* @author Mark Paluch
30+
* @author Christoph Strobl
3231
*/
3332
public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchEntityInformationCreator {
3433

3534
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
3635

3736
public ElasticsearchEntityInformationCreatorImpl(
3837
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
39-
38+
4039
Assert.notNull(mappingContext, "MappingContext must not be null!");
41-
40+
4241
this.mappingContext = mappingContext;
4342
}
4443

4544
@Override
4645
@SuppressWarnings("unchecked")
47-
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
46+
public <T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
4847

4948
ElasticsearchPersistentEntity<T> persistentEntity = (ElasticsearchPersistentEntity<T>) mappingContext
5049
.getRequiredPersistentEntity(domainClass);

src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static org.springframework.data.querydsl.QuerydslUtils.*;
1919

20-
import java.io.Serializable;
2120
import java.lang.reflect.Method;
2221
import java.util.Optional;
2322
import java.util.UUID;
@@ -47,23 +46,24 @@
4746
* @author Ryan Henszey
4847
* @author Gad Akuka
4948
* @author Mark Paluch
49+
* @author Christoph Strobl
5050
*/
5151
public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
5252

5353
private final ElasticsearchOperations elasticsearchOperations;
5454
private final ElasticsearchEntityInformationCreator entityInformationCreator;
5555

5656
public ElasticsearchRepositoryFactory(ElasticsearchOperations elasticsearchOperations) {
57-
57+
5858
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
59-
59+
6060
this.elasticsearchOperations = elasticsearchOperations;
6161
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(
6262
elasticsearchOperations.getElasticsearchConverter().getMappingContext());
6363
}
6464

6565
@Override
66-
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
66+
public <T, ID> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
6767
return entityInformationCreator.getEntityInformation(domainClass);
6868
}
6969

src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import java.io.Serializable;
1918
import java.util.Optional;
2019

2120
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
@@ -34,9 +33,10 @@
3433
* @author Ryan Henszey
3534
* @author Oliver Gierke
3635
* @author Mark Paluch
36+
* @author Christoph Strobl
3737
*/
38-
public class MappingElasticsearchEntityInformation<T, ID extends Serializable>
39-
extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> {
38+
public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
39+
implements ElasticsearchEntityInformation<T, ID> {
4040

4141
private final ElasticsearchPersistentEntity<T> entityMetadata;
4242
private final String indexName;

src/test/java/org/springframework/data/elasticsearch/InnerObjectTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,17 +33,16 @@
3333

3434
/**
3535
* @author Mohsin Husen
36+
* @author Christoph Strobl
3637
*/
3738

3839
@RunWith(SpringJUnit4ClassRunner.class)
3940
@ContextConfiguration("classpath:/repository-test-nested-object.xml")
4041
public class InnerObjectTests {
4142

42-
@Autowired
43-
private SampleElasticSearchBookRepository bookRepository;
43+
@Autowired private SampleElasticSearchBookRepository bookRepository;
4444

45-
@Autowired
46-
private ElasticsearchTemplate elasticsearchTemplate;
45+
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
4746

4847
@Before
4948
public void before() {
@@ -67,6 +66,6 @@ public void shouldIndexInnerObject() {
6766
// when
6867
bookRepository.save(book);
6968
// then
70-
assertThat(bookRepository.findOne(id), is(notNullValue()));
69+
assertThat(bookRepository.findById(id), is(notNullValue()));
7170
}
7271
}

src/test/java/org/springframework/data/elasticsearch/immutable/ImmutableElasticsearchRepositoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Young Gu
3333
* @author Oliver Gierke
3434
* @author Mark Paluch
35+
* @author Christoph Strobl
3536
*/
3637
@RunWith(SpringJUnit4ClassRunner.class)
3738
@ContextConfiguration("classpath:immutable-repository-test.xml")
@@ -59,7 +60,7 @@ public void shouldSaveAndFindImmutableDocument() {
5960
assertThat(entity.getId(), is(notNullValue()));
6061

6162
// then
62-
Optional<ImmutableEntity> entityFromElasticSearch = repository.findOne(entity.getId());
63+
Optional<ImmutableEntity> entityFromElasticSearch = repository.findById(entity.getId());
6364

6465
assertThat(entityFromElasticSearch.isPresent(), is(true));
6566

0 commit comments

Comments
 (0)