30
30
import org .springframework .data .domain .PageImpl ;
31
31
import org .springframework .data .domain .Pageable ;
32
32
import org .springframework .data .domain .Sort ;
33
+ import org .springframework .data .domain .TypedExampleSpec ;
33
34
import org .springframework .data .mongodb .core .MongoOperations ;
34
35
import org .springframework .data .mongodb .core .MongoTemplate ;
35
36
import org .springframework .data .mongodb .core .query .Criteria ;
@@ -70,6 +71,7 @@ public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperat
70
71
* (non-Javadoc)
71
72
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
72
73
*/
74
+ @ Override
73
75
public <S extends T > S save (S entity ) {
74
76
75
77
Assert .notNull (entity , "Entity must not be null!" );
@@ -87,6 +89,7 @@ public <S extends T> S save(S entity) {
87
89
* (non-Javadoc)
88
90
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
89
91
*/
92
+ @ Override
90
93
public <S extends T > List <S > save (Iterable <S > entities ) {
91
94
92
95
Assert .notNull (entities , "The given Iterable of entities not be null!" );
@@ -116,6 +119,7 @@ public <S extends T> List<S> save(Iterable<S> entities) {
116
119
* (non-Javadoc)
117
120
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
118
121
*/
122
+ @ Override
119
123
public T findOne (ID id ) {
120
124
121
125
Assert .notNull (id , "The given id must not be null!" );
@@ -135,6 +139,7 @@ private Criteria getIdCriteria(Object id) {
135
139
* (non-Javadoc)
136
140
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
137
141
*/
142
+ @ Override
138
143
public boolean exists (ID id ) {
139
144
140
145
Assert .notNull (id , "The given id must not be null!" );
@@ -147,6 +152,7 @@ public boolean exists(ID id) {
147
152
* (non-Javadoc)
148
153
* @see org.springframework.data.repository.CrudRepository#count()
149
154
*/
155
+ @ Override
150
156
public long count () {
151
157
return mongoOperations .getCollection (entityInformation .getCollectionName ()).count ();
152
158
}
@@ -155,6 +161,7 @@ public long count() {
155
161
* (non-Javadoc)
156
162
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
157
163
*/
164
+ @ Override
158
165
public void delete (ID id ) {
159
166
160
167
Assert .notNull (id , "The given id must not be null!" );
@@ -166,6 +173,7 @@ public void delete(ID id) {
166
173
* (non-Javadoc)
167
174
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
168
175
*/
176
+ @ Override
169
177
public void delete (T entity ) {
170
178
171
179
Assert .notNull (entity , "The given entity must not be null!" );
@@ -177,6 +185,7 @@ public void delete(T entity) {
177
185
* (non-Javadoc)
178
186
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
179
187
*/
188
+ @ Override
180
189
public void delete (Iterable <? extends T > entities ) {
181
190
182
191
Assert .notNull (entities , "The given Iterable of entities not be null!" );
@@ -190,6 +199,7 @@ public void delete(Iterable<? extends T> entities) {
190
199
* (non-Javadoc)
191
200
* @see org.springframework.data.repository.CrudRepository#deleteAll()
192
201
*/
202
+ @ Override
193
203
public void deleteAll () {
194
204
mongoOperations .remove (new Query (), entityInformation .getCollectionName ());
195
205
}
@@ -198,6 +208,7 @@ public void deleteAll() {
198
208
* (non-Javadoc)
199
209
* @see org.springframework.data.repository.CrudRepository#findAll()
200
210
*/
211
+ @ Override
201
212
public List <T > findAll () {
202
213
return findAll (new Query ());
203
214
}
@@ -206,6 +217,7 @@ public List<T> findAll() {
206
217
* (non-Javadoc)
207
218
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
208
219
*/
220
+ @ Override
209
221
public Iterable <T > findAll (Iterable <ID > ids ) {
210
222
211
223
Set <ID > parameters = new HashSet <ID >(tryDetermineRealSizeOrReturn (ids , 10 ));
@@ -220,6 +232,7 @@ public Iterable<T> findAll(Iterable<ID> ids) {
220
232
* (non-Javadoc)
221
233
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
222
234
*/
235
+ @ Override
223
236
public Page <T > findAll (final Pageable pageable ) {
224
237
225
238
Long count = count ();
@@ -232,6 +245,7 @@ public Page<T> findAll(final Pageable pageable) {
232
245
* (non-Javadoc)
233
246
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
234
247
*/
248
+ @ Override
235
249
public List <T > findAll (Sort sort ) {
236
250
return findAll (new Query ().with (sort ));
237
251
}
@@ -273,26 +287,26 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
273
287
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
274
288
*/
275
289
@ Override
276
- public <S extends T > Page <T > findAll (Example <S > example , Pageable pageable ) {
290
+ public <S extends T > Page <S > findAll (Example <S > example , Pageable pageable ) {
277
291
278
292
Assert .notNull (example , "Sample must not be null!" );
279
293
280
294
Query q = new Query (new Criteria ().alike (example )).with (pageable );
281
295
282
- long count = mongoOperations .count (q , entityInformation . getJavaType ( ), entityInformation . getCollectionName ());
296
+ long count = mongoOperations .count (q , getResultType ( example ), getCollectionName (example ));
283
297
if (count == 0 ) {
284
- return new PageImpl <T >(Collections .<T > emptyList ());
298
+ return new PageImpl <S >(Collections .<S > emptyList ());
285
299
}
286
- return new PageImpl <T >(mongoOperations .find (q , entityInformation . getJavaType ( ),
287
- entityInformation . getCollectionName ()), pageable , count );
300
+ return new PageImpl <S >(mongoOperations .find (q , getResultType ( example ), getCollectionName ( example ) ),
301
+ pageable , count );
288
302
}
289
303
290
304
/*
291
305
* (non-Javadoc)
292
306
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
293
307
*/
294
308
@ Override
295
- public <S extends T > List <T > findAll (Example <S > example , Sort sort ) {
309
+ public <S extends T > List <S > findAll (Example <S > example , Sort sort ) {
296
310
297
311
Assert .notNull (example , "Sample must not be null!" );
298
312
@@ -302,30 +316,25 @@ public <S extends T> List<T> findAll(Example<S> example, Sort sort) {
302
316
q .with (sort );
303
317
}
304
318
305
- return findAll ( q );
319
+ return mongoOperations . find ( q , getResultType ( example ), getCollectionName ( example ) );
306
320
}
307
321
308
322
/*
309
323
* (non-Javadoc)
310
324
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)
311
325
*/
312
326
@ Override
313
- public <S extends T > List <T > findAll (Example <S > example ) {
314
-
315
- Assert .notNull (example , "Sample must not be null!" );
316
-
317
- Query q = new Query (new Criteria ().alike (example ));
318
-
319
- return findAll (q );
327
+ public <S extends T > List <S > findAll (Example <S > example ) {
328
+ return findAll (example , (Sort ) null );
320
329
}
321
330
322
331
@ Override
323
- public <S extends T > T findOne (Example <S > example ) {
332
+ public <S extends T > S findOne (Example <S > example ) {
324
333
325
334
Assert .notNull (example , "Sample must not be null!" );
326
335
327
336
Query q = new Query (new Criteria ().alike (example ));
328
- return mongoOperations .findOne (q , entityInformation . getJavaType ( ), entityInformation . getCollectionName ());
337
+ return mongoOperations .findOne (q , getResultType ( example ), getCollectionName (example ));
329
338
}
330
339
331
340
@ Override
@@ -334,7 +343,7 @@ public <S extends T> long count(Example<S> example) {
334
343
Assert .notNull (example , "Sample must not be null!" );
335
344
336
345
Query q = new Query (new Criteria ().alike (example ));
337
- return mongoOperations .count (q , entityInformation . getJavaType ( ), entityInformation . getCollectionName ());
346
+ return mongoOperations .count (q , getResultType ( example ), getCollectionName (example ));
338
347
}
339
348
340
349
@ Override
@@ -343,7 +352,20 @@ public <S extends T> boolean exists(Example<S> example) {
343
352
Assert .notNull (example , "Sample must not be null!" );
344
353
345
354
Query q = new Query (new Criteria ().alike (example ));
346
- return mongoOperations .exists (q , entityInformation .getJavaType (), entityInformation .getCollectionName ());
355
+ return mongoOperations .exists (q , getResultType (example ), getCollectionName (example ));
356
+ }
357
+
358
+ private <S extends T > Class <S > getResultType (Example <S > example ) {
359
+
360
+ if (example .getExampleSpec () instanceof TypedExampleSpec <?>){
361
+ return example .getResultType ();
362
+ }
363
+
364
+ return (Class <S >) entityInformation .getJavaType ();
365
+ }
366
+
367
+ private String getCollectionName (Example <?> example ) {
368
+ return entityInformation .getCollectionName ();
347
369
}
348
370
349
371
private List <T > findAll (Query query ) {
0 commit comments