@@ -73,6 +73,10 @@ public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperat
73
73
this .mongoOperations = mongoOperations ;
74
74
}
75
75
76
+ // -------------------------------------------------------------------------
77
+ // Methods from CrudRepository
78
+ // -------------------------------------------------------------------------
79
+
76
80
/*
77
81
* (non-Javadoc)
78
82
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
@@ -136,6 +140,27 @@ public boolean existsById(ID id) {
136
140
entityInformation .getCollectionName ());
137
141
}
138
142
143
+ /*
144
+ * (non-Javadoc)
145
+ * @see org.springframework.data.repository.CrudRepository#findAll()
146
+ */
147
+ @ Override
148
+ public List <T > findAll () {
149
+ return findAll (new Query ());
150
+ }
151
+
152
+ /*
153
+ * (non-Javadoc)
154
+ * @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
155
+ */
156
+ @ Override
157
+ public Iterable <T > findAllById (Iterable <ID > ids ) {
158
+
159
+ Assert .notNull (ids , "The given Ids of entities not be null!" );
160
+
161
+ return findAll (getIdQuery (ids ));
162
+ }
163
+
139
164
/*
140
165
* (non-Javadoc)
141
166
* @see org.springframework.data.repository.CrudRepository#count()
@@ -178,55 +203,42 @@ public void delete(T entity) {
178
203
179
204
/*
180
205
* (non-Javadoc)
181
- * @see org.springframework.data.repository.CrudRepository#delete (java.lang.Iterable)
206
+ * @see org.springframework.data.repository.CrudRepository#deleteAllById (java.lang.Iterable)
182
207
*/
183
- @ Override
184
- public void deleteAll (Iterable <? extends T > entities ) {
185
-
186
- Assert .notNull (entities , "The given Iterable of entities must not be null!" );
187
-
188
- entities .forEach (this ::delete );
189
- }
190
-
191
208
@ Override
192
209
public void deleteAllById (Iterable <? extends ID > ids ) {
193
210
194
211
Assert .notNull (ids , "The given Iterable of ids must not be null!" );
195
212
196
- ids .forEach (this ::deleteById );
213
+ mongoOperations .remove (getIdQuery ((Iterable <? extends ID >) ids ), entityInformation .getJavaType (),
214
+ entityInformation .getCollectionName ());
197
215
}
198
216
199
217
/*
200
218
* (non-Javadoc)
201
- * @see org.springframework.data.repository.CrudRepository#deleteAll( )
219
+ * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable )
202
220
*/
203
221
@ Override
204
- public void deleteAll () {
205
- mongoOperations .remove (new Query (), entityInformation .getCollectionName ());
206
- }
222
+ public void deleteAll (Iterable <? extends T > entities ) {
207
223
208
- /*
209
- * (non-Javadoc)
210
- * @see org.springframework.data.repository.CrudRepository#findAll()
211
- */
212
- @ Override
213
- public List <T > findAll () {
214
- return findAll (new Query ());
224
+ Assert .notNull (entities , "The given Iterable of entities must not be null!" );
225
+
226
+ entities .forEach (this ::delete );
215
227
}
216
228
217
229
/*
218
230
* (non-Javadoc)
219
- * @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable )
231
+ * @see org.springframework.data.repository.CrudRepository#deleteAll( )
220
232
*/
221
233
@ Override
222
- public Iterable <T > findAllById (Iterable <ID > ids ) {
223
-
224
- Assert .notNull (ids , "The given Ids of entities not be null!" );
225
-
226
- return findAll (new Query (new Criteria (entityInformation .getIdAttribute ())
227
- .in (Streamable .of (ids ).stream ().collect (StreamUtils .toUnmodifiableList ()))));
234
+ public void deleteAll () {
235
+ mongoOperations .remove (new Query (), entityInformation .getCollectionName ());
228
236
}
229
237
238
+ // -------------------------------------------------------------------------
239
+ // Methods from PagingAndSortingRepository
240
+ // -------------------------------------------------------------------------
241
+
230
242
/*
231
243
* (non-Javadoc)
232
244
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
@@ -254,6 +266,10 @@ public List<T> findAll(Sort sort) {
254
266
return findAll (new Query ().with (sort ));
255
267
}
256
268
269
+ // -------------------------------------------------------------------------
270
+ // Methods from MongoRepository
271
+ // -------------------------------------------------------------------------
272
+
257
273
/*
258
274
* (non-Javadoc)
259
275
* @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Object)
@@ -284,23 +300,33 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
284
300
return new ArrayList <>(mongoOperations .insertAll (list ));
285
301
}
286
302
303
+ // -------------------------------------------------------------------------
304
+ // Methods from QueryByExampleExecutor
305
+ // -------------------------------------------------------------------------
306
+
287
307
/*
288
308
* (non-Javadoc)
289
- * @see org.springframework.data.mongodb. repository.MongoRepository#findAllByExample (org.springframework.data.domain.Example, org.springframework.data.domain.Pageable )
309
+ * @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne (org.springframework.data.domain.Example)
290
310
*/
291
311
@ Override
292
- public <S extends T > Page <S > findAll ( final Example <S > example , Pageable pageable ) {
312
+ public <S extends T > Optional <S > findOne ( Example <S > example ) {
293
313
294
314
Assert .notNull (example , "Sample must not be null!" );
295
- Assert .notNull (pageable , "Pageable must not be null!" );
296
315
297
316
Query query = new Query (new Criteria ().alike (example )) //
298
- .collation (entityInformation .getCollation ()). with ( pageable ); //
317
+ .collation (entityInformation .getCollation ());
299
318
300
- List <S > list = mongoOperations .find (query , example .getProbeType (), entityInformation .getCollectionName ());
319
+ return Optional
320
+ .ofNullable (mongoOperations .findOne (query , example .getProbeType (), entityInformation .getCollectionName ()));
321
+ }
301
322
302
- return PageableExecutionUtils .getPage (list , pageable ,
303
- () -> mongoOperations .count (Query .of (query ).limit (-1 ).skip (-1 ), example .getProbeType (), entityInformation .getCollectionName ()));
323
+ /*
324
+ * (non-Javadoc)
325
+ * @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)
326
+ */
327
+ @ Override
328
+ public <S extends T > List <S > findAll (Example <S > example ) {
329
+ return findAll (example , Sort .unsorted ());
304
330
}
305
331
306
332
/*
@@ -322,27 +348,21 @@ public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
322
348
323
349
/*
324
350
* (non-Javadoc)
325
- * @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)
326
- */
327
- @ Override
328
- public <S extends T > List <S > findAll (Example <S > example ) {
329
- return findAll (example , Sort .unsorted ());
330
- }
331
-
332
- /*
333
- * (non-Javadoc)
334
- * @see org.springframework.data.repository.query.QueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
351
+ * @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
335
352
*/
336
353
@ Override
337
- public <S extends T > Optional <S > findOne (Example <S > example ) {
354
+ public <S extends T > Page <S > findAll (Example <S > example , Pageable pageable ) {
338
355
339
356
Assert .notNull (example , "Sample must not be null!" );
357
+ Assert .notNull (pageable , "Pageable must not be null!" );
340
358
341
359
Query query = new Query (new Criteria ().alike (example )) //
342
- .collation (entityInformation .getCollation ());
360
+ .collation (entityInformation .getCollation ()). with ( pageable ); //
343
361
344
- return Optional
345
- .ofNullable (mongoOperations .findOne (query , example .getProbeType (), entityInformation .getCollectionName ()));
362
+ List <S > list = mongoOperations .find (query , example .getProbeType (), entityInformation .getCollectionName ());
363
+
364
+ return PageableExecutionUtils .getPage (list , pageable ,
365
+ () -> mongoOperations .count (Query .of (query ).limit (-1 ).skip (-1 ), example .getProbeType (), entityInformation .getCollectionName ()));
346
366
}
347
367
348
368
/*
@@ -375,6 +395,10 @@ public <S extends T> boolean exists(Example<S> example) {
375
395
return mongoOperations .exists (query , example .getProbeType (), entityInformation .getCollectionName ());
376
396
}
377
397
398
+ // -------------------------------------------------------------------------
399
+ // Utility methods
400
+ // -------------------------------------------------------------------------
401
+
378
402
private Query getIdQuery (Object id ) {
379
403
return new Query (getIdCriteria (id ));
380
404
}
@@ -383,6 +407,11 @@ private Criteria getIdCriteria(Object id) {
383
407
return where (entityInformation .getIdAttribute ()).is (id );
384
408
}
385
409
410
+ private Query getIdQuery (Iterable <? extends ID > ids ) {
411
+ return new Query (new Criteria (entityInformation .getIdAttribute ())
412
+ .in (Streamable .of (ids ).stream ().collect (StreamUtils .toUnmodifiableList ())));
413
+ }
414
+
386
415
private List <T > findAll (@ Nullable Query query ) {
387
416
388
417
if (query == null ) {
@@ -391,4 +420,5 @@ private List<T> findAll(@Nullable Query query) {
391
420
392
421
return mongoOperations .find (query , entityInformation .getJavaType (), entityInformation .getCollectionName ());
393
422
}
423
+
394
424
}
0 commit comments