Skip to content

Commit e323859

Browse files
committed
DATAMONGO-1679 - Adapt to API changes in repository interfaces.
1 parent 5d8370f commit e323859

30 files changed

+459
-581
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ protected Object convertSimpleOrDocument(Object source, MongoPersistentEntity<?>
422422
return getMappedObject((BasicDBObject) source, entity);
423423
}
424424

425-
if(source instanceof BsonValue) {
425+
if (source instanceof BsonValue) {
426426
return source;
427427
}
428428

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18+
import lombok.EqualsAndHashCode;
19+
1820
import java.util.HashMap;
1921
import java.util.Map;
2022
import java.util.Map.Entry;
2123

2224
import org.bson.Document;
2325
import org.springframework.util.Assert;
24-
import org.springframework.util.ObjectUtils;
2526

2627
/**
2728
* @author Thomas Risberg
@@ -30,6 +31,7 @@
3031
* @author Christoph Strobl
3132
* @author Mark Paluch
3233
*/
34+
@EqualsAndHashCode
3335
public class Field {
3436

3537
private final Map<String, Integer> criteria = new HashMap<String, Integer>();
@@ -83,6 +85,7 @@ public Field position(String field, int value) {
8385

8486
public Document getFieldsObject() {
8587

88+
@SuppressWarnings({ "unchecked", "rawtypes" })
8689
Document document = new Document((Map) criteria);
8790

8891
for (Entry<String, Object> entry : slices.entrySet()) {
@@ -99,58 +102,4 @@ public Document getFieldsObject() {
99102

100103
return document;
101104
}
102-
103-
/*
104-
* (non-Javadoc)
105-
* @see java.lang.Object#equals(java.lang.Object)
106-
*/
107-
@Override
108-
public boolean equals(Object object) {
109-
110-
if (this == object) {
111-
return true;
112-
}
113-
114-
if (!(object instanceof Field)) {
115-
return false;
116-
}
117-
118-
Field that = (Field) object;
119-
120-
if (!this.criteria.equals(that.criteria)) {
121-
return false;
122-
}
123-
124-
if (!this.slices.equals(that.slices)) {
125-
return false;
126-
}
127-
128-
if (!this.elemMatchs.equals(that.elemMatchs)) {
129-
return false;
130-
}
131-
132-
boolean samePositionKey = this.postionKey == null ? that.postionKey == null
133-
: this.postionKey.equals(that.postionKey);
134-
boolean samePositionValue = this.positionValue == that.positionValue;
135-
136-
return samePositionKey && samePositionValue;
137-
}
138-
139-
/*
140-
* (non-Javadoc)
141-
* @see java.lang.Object#hashCode()
142-
*/
143-
@Override
144-
public int hashCode() {
145-
146-
int result = 17;
147-
148-
result += 31 * ObjectUtils.nullSafeHashCode(this.criteria);
149-
result += 31 * ObjectUtils.nullSafeHashCode(this.elemMatchs);
150-
result += 31 * ObjectUtils.nullSafeHashCode(this.slices);
151-
result += 31 * ObjectUtils.nullSafeHashCode(this.postionKey);
152-
result += 31 * ObjectUtils.nullSafeHashCode(this.positionValue);
153-
154-
return result;
155-
}
156105
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.data.domain.Sort.Order;
3434
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
3535
import org.springframework.util.Assert;
36-
import org.springframework.util.ObjectUtils;
3736

3837
/**
3938
* @author Thomas Risberg
@@ -48,8 +47,8 @@ public class Query {
4847

4948
private final Set<Class<?>> restrictedTypes = new HashSet<Class<?>>();
5049
private final Map<String, CriteriaDefinition> criteria = new LinkedHashMap<String, CriteriaDefinition>();
51-
private Field fieldSpec;
52-
private Sort sort;
50+
private Field fieldSpec = null;
51+
private Sort sort = Sort.unsorted();
5352
private long skip;
5453
private int limit;
5554
private String hint;
@@ -103,9 +102,11 @@ public Query addCriteria(CriteriaDefinition criteriaDefinition) {
103102
}
104103

105104
public Field fields() {
106-
if (fieldSpec == null) {
105+
106+
if (this.fieldSpec == null) {
107107
this.fieldSpec = new Field();
108108
}
109+
109110
return this.fieldSpec;
110111
}
111112

@@ -170,22 +171,19 @@ public Query with(Pageable pageable) {
170171
*/
171172
public Query with(Sort sort) {
172173

173-
if (sort == null || ObjectUtils.nullSafeEquals(Sort.unsorted(), sort)) {
174+
Assert.notNull(sort, "Sort must not be null!");
175+
176+
if (sort.isUnsorted()) {
174177
return this;
175178
}
176179

177-
for (Order order : sort) {
178-
if (order.isIgnoreCase()) {
179-
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
180-
+ "MongoDB does not support sorting ignoreing case currently!", order.getProperty()));
181-
}
182-
}
180+
sort.stream().filter(Order::isIgnoreCase).findFirst().ifPresent(it -> {
183181

184-
if (this.sort == null) {
185-
this.sort = sort;
186-
} else {
187-
this.sort = this.sort.and(sort);
188-
}
182+
throw new IllegalArgumentException(String.format("Given sort contained an Order for %s with ignore case! "
183+
+ "MongoDB does not support sorting ignoring case currently!", it.getProperty()));
184+
});
185+
186+
this.sort = this.sort.and(sort);
189187

190188
return this;
191189
}
@@ -238,15 +236,14 @@ public Document getFieldsObject() {
238236

239237
public Document getSortObject() {
240238

241-
if (this.sort == null) {
239+
if (this.sort.isUnsorted()) {
242240
return null;
243241
}
244242

245243
Document document = new Document();
246244

247-
for (org.springframework.data.domain.Sort.Order order : this.sort) {
248-
document.put(order.getProperty(), order.isAscending() ? 1 : -1);
249-
}
245+
this.sort.stream()//
246+
.forEach(order -> document.put(order.getProperty(), order.isAscending() ? 1 : -1));
250247

251248
return document;
252249
}
@@ -440,7 +437,7 @@ protected boolean querySettingsEquals(Query that) {
440437

441438
boolean criteriaEqual = this.criteria.equals(that.criteria);
442439
boolean fieldsEqual = nullSafeEquals(this.fieldSpec, that.fieldSpec);
443-
boolean sortEqual = nullSafeEquals(this.sort, that.sort);
440+
boolean sortEqual = this.sort.equals(that.sort);
444441
boolean hintEqual = nullSafeEquals(this.hint, that.hint);
445442
boolean skipEqual = this.skip == that.skip;
446443
boolean limitEqual = this.limit == that.limit;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public interface MongoRepository<T, ID extends Serializable>
3838

3939
/*
4040
* (non-Javadoc)
41-
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
41+
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
4242
*/
4343
@Override
44-
<S extends T> List<S> save(Iterable<S> entites);
44+
<S extends T> List<S> saveAll(Iterable<S> entites);
4545

4646
/*
4747
* (non-Javadoc)
@@ -58,9 +58,9 @@ public interface MongoRepository<T, ID extends Serializable>
5858
List<T> findAll(Sort sort);
5959

6060
/**
61-
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use
62-
* the returned instance for further operations as the save operation might have changed the entity instance
63-
* completely. Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
61+
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
62+
* returned instance for further operations as the save operation might have changed the entity instance completely.
63+
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
6464
*
6565
* @param entity must not be {@literal null}.
6666
* @return the saved entity

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18-
import java.io.Serializable;
18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
1920

2021
import org.reactivestreams.Publisher;
2122
import org.springframework.data.domain.Example;
2223
import org.springframework.data.domain.Sort;
2324
import org.springframework.data.repository.NoRepositoryBean;
24-
2525
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
26-
import reactor.core.publisher.Flux;
27-
import reactor.core.publisher.Mono;
2826

2927
/**
3028
* Mongo specific {@link org.springframework.data.repository.Repository} interface with reactive support.
@@ -33,44 +31,46 @@
3331
* @since 2.0
3432
*/
3533
@NoRepositoryBean
36-
public interface ReactiveMongoRepository<T, ID extends Serializable> extends ReactiveSortingRepository<T, ID> {
34+
public interface ReactiveMongoRepository<T, ID> extends ReactiveSortingRepository<T, ID> {
3735

3836
/**
39-
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use
40-
* the returned instance for further operations as the save operation might have changed the entity instance
41-
* completely. Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
37+
* Inserts the given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use the
38+
* returned instance for further operations as the save operation might have changed the entity instance completely.
39+
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
4240
*
4341
* @param entity must not be {@literal null}.
4442
* @return the saved entity
4543
*/
4644
<S extends T> Mono<S> insert(S entity);
4745

4846
/**
49-
* Inserts the given entities. Assumes the instance to be new to be able to apply insertion optimizations. Use
50-
* the returned instance for further operations as the save operation might have changed the entity instance
51-
* completely. Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
47+
* Inserts the given entities. Assumes the instance to be new to be able to apply insertion optimizations. Use the
48+
* returned instance for further operations as the save operation might have changed the entity instance completely.
49+
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
5250
*
5351
* @param entities must not be {@literal null}.
5452
* @return the saved entity
5553
*/
5654
<S extends T> Flux<S> insert(Iterable<S> entities);
5755

5856
/**
59-
* Inserts the given entities. Assumes the instance to be new to be able to apply insertion optimizations. Use
60-
* the returned instance for further operations as the save operation might have changed the entity instance
61-
* completely. Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
57+
* Inserts the given entities. Assumes the instance to be new to be able to apply insertion optimizations. Use the
58+
* returned instance for further operations as the save operation might have changed the entity instance completely.
59+
* Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
6260
*
6361
* @param entities must not be {@literal null}.
6462
* @return the saved entity
6563
*/
6664
<S extends T> Flux<S> insert(Publisher<S> entities);
6765

68-
/* (non-Javadoc)
66+
/*
67+
* (non-Javadoc)
6968
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
7069
*/
7170
<S extends T> Flux<S> findAll(Example<S> example);
7271

73-
/* (non-Javadoc)
72+
/*
73+
* (non-Javadoc)
7474
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
7575
*/
7676
<S extends T> Flux<S> findAll(Example<S> example, Sort sort);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoEntityInformation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

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

2220
/**
2321
* Mongo specific {@link EntityInformation}.
2422
*
2523
* @author Oliver Gierke
2624
*/
27-
public interface MongoEntityInformation<T, ID extends Serializable> extends EntityInformation<T, ID> {
25+
public interface MongoEntityInformation<T, ID> extends EntityInformation<T, ID> {
2826

2927
/**
3028
* Returns the name of the collection the entity shall be persisted to.
@@ -39,4 +37,4 @@ public interface MongoEntityInformation<T, ID extends Serializable> extends Enti
3937
* @return
4038
*/
4139
String getIdAttribute();
42-
}
40+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MappingMongoEntityInformation.java

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

18-
import java.io.Serializable;
19-
2018
import org.bson.types.ObjectId;
2119
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2220
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
@@ -30,7 +28,7 @@
3028
* @author Oliver Gierke
3129
* @author Christoph Strobl
3230
*/
33-
public class MappingMongoEntityInformation<T, ID extends Serializable> extends PersistentEntityInformation<T, ID>
31+
public class MappingMongoEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
3432
implements MongoEntityInformation<T, ID> {
3533

3634
private final MongoPersistentEntity<T> entityMetadata;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoEntityInformationSupport.java

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

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.domain.Persistable;
2119
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2220
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
@@ -42,8 +40,7 @@ private MongoEntityInformationSupport() {}
4240
* @return never {@literal null}.
4341
*/
4442
@SuppressWarnings("unchecked")
45-
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
46-
MongoPersistentEntity<?> entity, Class<?> idType) {
43+
static <T, ID> MongoEntityInformation<T, ID> entityInformationFor(MongoPersistentEntity<?> entity, Class<?> idType) {
4744

4845
Assert.notNull(entity, "Entity must not be null!");
4946

0 commit comments

Comments
 (0)