51
51
import org .springframework .data .annotation .Id ;
52
52
import org .springframework .data .authentication .UserCredentials ;
53
53
import org .springframework .data .convert .EntityReader ;
54
+ import org .springframework .data .domain .Example ;
54
55
import org .springframework .data .geo .Distance ;
55
56
import org .springframework .data .geo .GeoResult ;
56
57
import org .springframework .data .geo .GeoResults ;
@@ -340,8 +341,8 @@ public CloseableIterator<T> doInCollection(DBCollection collection) throws Mongo
340
341
DBCursor cursor = collection .find (mappedQuery , mappedFields );
341
342
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer (query , entityType );
342
343
343
- ReadDbObjectCallback <T > readCallback = new ReadDbObjectCallback <T >(mongoConverter , entityType ,
344
- collection .getName ());
344
+ ReadDbObjectCallback <T > readCallback = new ReadDbObjectCallback <T >(mongoConverter , entityType , collection
345
+ .getName ());
345
346
346
347
return new CloseableIterableCursorAdapter <T >(cursorPreparer .prepare (cursor ), exceptionTranslator , readCallback );
347
348
}
@@ -374,8 +375,8 @@ public CommandResult doInDB(DB db) throws MongoException, DataAccessException {
374
375
*/
375
376
@ Deprecated
376
377
public CommandResult executeCommand (final DBObject command , final int options ) {
377
- return executeCommand (command ,
378
- ( options & Bytes . QUERYOPTION_SLAVEOK ) != 0 ? ReadPreference . secondaryPreferred () : ReadPreference .primary ());
378
+ return executeCommand (command , ( options & Bytes . QUERYOPTION_SLAVEOK ) != 0 ? ReadPreference . secondaryPreferred ()
379
+ : ReadPreference .primary ());
379
380
}
380
381
381
382
/*
@@ -421,8 +422,7 @@ public void executeQuery(Query query, String collectionName, DocumentCallbackHan
421
422
* @param preparer allows for customization of the {@link DBCursor} used when iterating over the result set, (apply
422
423
* limits, skips and so on).
423
424
*/
424
- protected void executeQuery (Query query , String collectionName , DocumentCallbackHandler dch ,
425
- CursorPreparer preparer ) {
425
+ protected void executeQuery (Query query , String collectionName , DocumentCallbackHandler dch , CursorPreparer preparer ) {
426
426
427
427
Assert .notNull (query );
428
428
@@ -637,6 +637,17 @@ public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
637
637
return doFindOne (collectionName , new BasicDBObject (idKey , id ), null , entityClass );
638
638
}
639
639
640
+ public <S extends T , T > List <T > findByExample (S sample ) {
641
+ return findByExample (new Example <S >(sample ));
642
+ }
643
+
644
+ @ SuppressWarnings ("unchecked" )
645
+ public <S extends T , T > List <T > findByExample (Example <S > sample ) {
646
+
647
+ Assert .notNull (sample , "Sample object must not be null!" );
648
+ return (List <T >) find (new Query (new Criteria ().alike (sample )), sample .getSampleType ());
649
+ }
650
+
640
651
public <T > GeoResults <T > geoNear (NearQuery near , Class <T > entityClass ) {
641
652
return geoNear (near , entityClass , determineCollectionName (entityClass ));
642
653
}
@@ -672,8 +683,8 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
672
683
List <Object > results = (List <Object >) commandResult .get ("results" );
673
684
results = results == null ? Collections .emptyList () : results ;
674
685
675
- DbObjectCallback <GeoResult <T >> callback = new GeoNearResultDbObjectCallback <T >(
676
- new ReadDbObjectCallback < T >( mongoConverter , entityClass , collectionName ), near .getMetric ());
686
+ DbObjectCallback <GeoResult <T >> callback = new GeoNearResultDbObjectCallback <T >(new ReadDbObjectCallback < T >(
687
+ mongoConverter , entityClass , collectionName ), near .getMetric ());
677
688
List <GeoResult <T >> result = new ArrayList <GeoResult <T >>(results .size ());
678
689
679
690
int index = 0 ;
@@ -749,9 +760,8 @@ public long count(final Query query, String collectionName) {
749
760
public long count (Query query , Class <?> entityClass , String collectionName ) {
750
761
751
762
Assert .hasText (collectionName );
752
- final DBObject dbObject = query == null ? null
753
- : queryMapper .getMappedObject (query .getQueryObject (),
754
- entityClass == null ? null : mappingContext .getPersistentEntity (entityClass ));
763
+ final DBObject dbObject = query == null ? null : queryMapper .getMappedObject (query .getQueryObject (),
764
+ entityClass == null ? null : mappingContext .getPersistentEntity (entityClass ));
755
765
756
766
return execute (collectionName , new CollectionCallback <Long >() {
757
767
public Long doInCollection (DBCollection collection ) throws MongoException , DataAccessException {
@@ -1030,8 +1040,8 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
1030
1040
MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .INSERT , collectionName ,
1031
1041
entityClass , dbDoc , null );
1032
1042
WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1033
- WriteResult writeResult = writeConcernToUse == null ? collection .insert (dbDoc )
1034
- : collection . insert ( dbDoc , writeConcernToUse );
1043
+ WriteResult writeResult = writeConcernToUse == null ? collection .insert (dbDoc ) : collection . insert ( dbDoc ,
1044
+ writeConcernToUse );
1035
1045
handleAnyWriteResultErrors (writeResult , dbDoc , MongoActionOperation .INSERT );
1036
1046
return dbDoc .get (ID_FIELD );
1037
1047
}
@@ -1052,8 +1062,8 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
1052
1062
MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .INSERT_LIST , collectionName , null ,
1053
1063
null , null );
1054
1064
WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1055
- WriteResult writeResult = writeConcernToUse == null ? collection .insert (dbDocList )
1056
- : collection . insert ( dbDocList .toArray ((DBObject []) new BasicDBObject [dbDocList .size ()]), writeConcernToUse );
1065
+ WriteResult writeResult = writeConcernToUse == null ? collection .insert (dbDocList ) : collection . insert (
1066
+ dbDocList .toArray ((DBObject []) new BasicDBObject [dbDocList .size ()]), writeConcernToUse );
1057
1067
handleAnyWriteResultErrors (writeResult , null , MongoActionOperation .INSERT_LIST );
1058
1068
return null ;
1059
1069
}
@@ -1083,8 +1093,8 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
1083
1093
MongoAction mongoAction = new MongoAction (writeConcern , MongoActionOperation .SAVE , collectionName , entityClass ,
1084
1094
dbDoc , null );
1085
1095
WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1086
- WriteResult writeResult = writeConcernToUse == null ? collection .save (dbDoc )
1087
- : collection . save ( dbDoc , writeConcernToUse );
1096
+ WriteResult writeResult = writeConcernToUse == null ? collection .save (dbDoc ) : collection . save ( dbDoc ,
1097
+ writeConcernToUse );
1088
1098
handleAnyWriteResultErrors (writeResult , dbDoc , MongoActionOperation .SAVE );
1089
1099
return dbDoc .get (ID_FIELD );
1090
1100
}
@@ -1137,10 +1147,10 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
1137
1147
1138
1148
increaseVersionForUpdateIfNecessary (entity , update );
1139
1149
1140
- DBObject queryObj = query == null ? new BasicDBObject ()
1141
- : queryMapper . getMappedObject ( query . getQueryObject (), entity );
1142
- DBObject updateObj = update == null ? new BasicDBObject ()
1143
- : updateMapper . getMappedObject ( update .getUpdateObject (), entity );
1150
+ DBObject queryObj = query == null ? new BasicDBObject () : queryMapper . getMappedObject ( query . getQueryObject (),
1151
+ entity );
1152
+ DBObject updateObj = update == null ? new BasicDBObject () : updateMapper . getMappedObject (
1153
+ update .getUpdateObject (), entity );
1144
1154
1145
1155
if (LOGGER .isDebugEnabled ()) {
1146
1156
LOGGER .debug ("Calling update using query: {} and update: {} in collection: {}" ,
@@ -1281,9 +1291,9 @@ private void assertUpdateableIdIfNotSet(Object entity) {
1281
1291
Object idValue = persistentEntity .getPropertyAccessor (entity ).getProperty (idProperty );
1282
1292
1283
1293
if (idValue == null && !MongoSimpleTypes .AUTOGENERATED_ID_TYPES .contains (idProperty .getType ())) {
1284
- throw new InvalidDataAccessApiUsageException (
1285
- String . format ( "Cannot autogenerate id of type %s for entity of type %s!" , idProperty .getType ().getName (),
1286
- entity . getClass () .getName ()));
1294
+ throw new InvalidDataAccessApiUsageException (String . format (
1295
+ "Cannot autogenerate id of type %s for entity of type %s!" , idProperty .getType ().getName (), entity . getClass ()
1296
+ .getName ()));
1287
1297
}
1288
1298
}
1289
1299
@@ -1322,12 +1332,12 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
1322
1332
WriteConcern writeConcernToUse = prepareWriteConcern (mongoAction );
1323
1333
1324
1334
if (LOGGER .isDebugEnabled ()) {
1325
- LOGGER .debug ("Remove using query: {} in collection: {}." ,
1326
- new Object [] { serializeToJsonSafely ( dboq ), collection .getName () });
1335
+ LOGGER .debug ("Remove using query: {} in collection: {}." , new Object [] { serializeToJsonSafely ( dboq ),
1336
+ collection .getName () });
1327
1337
}
1328
1338
1329
- WriteResult wr = writeConcernToUse == null ? collection .remove (dboq )
1330
- : collection . remove ( dboq , writeConcernToUse );
1339
+ WriteResult wr = writeConcernToUse == null ? collection .remove (dboq ) : collection . remove ( dboq ,
1340
+ writeConcernToUse );
1331
1341
1332
1342
handleAnyWriteResultErrors (wr , dboq , MongoActionOperation .REMOVE );
1333
1343
@@ -1343,8 +1353,8 @@ public <T> List<T> findAll(Class<T> entityClass) {
1343
1353
}
1344
1354
1345
1355
public <T > List <T > findAll (Class <T > entityClass , String collectionName ) {
1346
- return executeFindMultiInternal (new FindCallback (null ), null ,
1347
- new ReadDbObjectCallback < T >( mongoConverter , entityClass , collectionName ), collectionName );
1356
+ return executeFindMultiInternal (new FindCallback (null ), null , new ReadDbObjectCallback < T >( mongoConverter ,
1357
+ entityClass , collectionName ), collectionName );
1348
1358
}
1349
1359
1350
1360
public <T > MapReduceResults <T > mapReduce (String inputCollectionName , String mapFunction , String reduceFunction ,
@@ -1360,8 +1370,8 @@ public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapF
1360
1370
1361
1371
public <T > MapReduceResults <T > mapReduce (Query query , String inputCollectionName , String mapFunction ,
1362
1372
String reduceFunction , Class <T > entityClass ) {
1363
- return mapReduce (query , inputCollectionName , mapFunction , reduceFunction , new MapReduceOptions (). outputTypeInline (),
1364
- entityClass );
1373
+ return mapReduce (query , inputCollectionName , mapFunction , reduceFunction ,
1374
+ new MapReduceOptions (). outputTypeInline (), entityClass );
1365
1375
}
1366
1376
1367
1377
public <T > MapReduceResults <T > mapReduce (Query query , String inputCollectionName , String mapFunction ,
@@ -1372,9 +1382,8 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
1372
1382
DBCollection inputCollection = getCollection (inputCollectionName );
1373
1383
1374
1384
MapReduceCommand command = new MapReduceCommand (inputCollection , mapFunc , reduceFunc ,
1375
- mapReduceOptions .getOutputCollection (), mapReduceOptions .getOutputType (),
1376
- query == null || query .getQueryObject () == null ? null
1377
- : queryMapper .getMappedObject (query .getQueryObject (), null ));
1385
+ mapReduceOptions .getOutputCollection (), mapReduceOptions .getOutputType (), query == null
1386
+ || query .getQueryObject () == null ? null : queryMapper .getMappedObject (query .getQueryObject (), null ));
1378
1387
1379
1388
copyMapReduceOptionsToCommand (query , mapReduceOptions , command );
1380
1389
@@ -1710,8 +1719,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
1710
1719
mappedFields , entityClass , collectionName );
1711
1720
}
1712
1721
1713
- return executeFindOneInternal (new FindOneCallback (mappedQuery , mappedFields ),
1714
- new ReadDbObjectCallback < T >( this .mongoConverter , entityClass , collectionName ), collectionName );
1722
+ return executeFindOneInternal (new FindOneCallback (mappedQuery , mappedFields ), new ReadDbObjectCallback < T >(
1723
+ this .mongoConverter , entityClass , collectionName ), collectionName );
1715
1724
}
1716
1725
1717
1726
/**
@@ -1725,8 +1734,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
1725
1734
* @return the List of converted objects.
1726
1735
*/
1727
1736
protected <T > List <T > doFind (String collectionName , DBObject query , DBObject fields , Class <T > entityClass ) {
1728
- return doFind (collectionName , query , fields , entityClass , null ,
1729
- new ReadDbObjectCallback < T >( this . mongoConverter , entityClass , collectionName ));
1737
+ return doFind (collectionName , query , fields , entityClass , null , new ReadDbObjectCallback < T >( this . mongoConverter ,
1738
+ entityClass , collectionName ));
1730
1739
}
1731
1740
1732
1741
/**
@@ -1744,8 +1753,8 @@ protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fie
1744
1753
*/
1745
1754
protected <T > List <T > doFind (String collectionName , DBObject query , DBObject fields , Class <T > entityClass ,
1746
1755
CursorPreparer preparer ) {
1747
- return doFind (collectionName , query , fields , entityClass , preparer ,
1748
- new ReadDbObjectCallback < T >( mongoConverter , entityClass , collectionName ));
1756
+ return doFind (collectionName , query , fields , entityClass , preparer , new ReadDbObjectCallback < T >( mongoConverter ,
1757
+ entityClass , collectionName ));
1749
1758
}
1750
1759
1751
1760
protected <S , T > List <T > doFind (String collectionName , DBObject query , DBObject fields , Class <S > entityClass ,
@@ -1898,8 +1907,8 @@ private <T> T executeFindOneInternal(CollectionCallback<DBObject> collectionCall
1898
1907
DbObjectCallback <T > objectCallback , String collectionName ) {
1899
1908
1900
1909
try {
1901
- T result = objectCallback
1902
- . doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb (), collectionName )));
1910
+ T result = objectCallback . doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb (),
1911
+ collectionName )));
1903
1912
return result ;
1904
1913
} catch (RuntimeException e ) {
1905
1914
throw potentiallyConvertRuntimeException (e , exceptionTranslator );
@@ -1924,8 +1933,8 @@ private <T> T executeFindOneInternal(CollectionCallback<DBObject> collectionCall
1924
1933
* @param collectionName the collection to be queried
1925
1934
* @return
1926
1935
*/
1927
- private <T > List <T > executeFindMultiInternal (CollectionCallback <DBCursor > collectionCallback , CursorPreparer preparer ,
1928
- DbObjectCallback <T > objectCallback , String collectionName ) {
1936
+ private <T > List <T > executeFindMultiInternal (CollectionCallback <DBCursor > collectionCallback ,
1937
+ CursorPreparer preparer , DbObjectCallback <T > objectCallback , String collectionName ) {
1929
1938
1930
1939
try {
1931
1940
@@ -2015,8 +2024,8 @@ String determineCollectionName(Class<?> entityClass) {
2015
2024
2016
2025
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
2017
2026
if (entity == null ) {
2018
- throw new InvalidDataAccessApiUsageException (
2019
- "No Persistent Entity information found for the class " + entityClass .getName ());
2027
+ throw new InvalidDataAccessApiUsageException ("No Persistent Entity information found for the class "
2028
+ + entityClass .getName ());
2020
2029
}
2021
2030
return entity .getCollection ();
2022
2031
}
@@ -2080,8 +2089,8 @@ private void handleCommandError(CommandResult result, DBObject source) {
2080
2089
String error = result .getErrorMessage ();
2081
2090
error = error == null ? "NO MESSAGE" : error ;
2082
2091
2083
- throw new InvalidDataAccessApiUsageException (
2084
- "Command execution failed: Error [" + error + "], Command = " + source , ex );
2092
+ throw new InvalidDataAccessApiUsageException ("Command execution failed: Error [" + error + "], Command = "
2093
+ + source , ex );
2085
2094
}
2086
2095
}
2087
2096
@@ -2277,8 +2286,7 @@ public T doWith(DBObject object) {
2277
2286
2278
2287
class UnwrapAndReadDbObjectCallback <T > extends ReadDbObjectCallback <T > {
2279
2288
2280
- public UnwrapAndReadDbObjectCallback (EntityReader <? super T , DBObject > reader , Class <T > type ,
2281
- String collectionName ) {
2289
+ public UnwrapAndReadDbObjectCallback (EntityReader <? super T , DBObject > reader , Class <T > type , String collectionName ) {
2282
2290
super (reader , type , collectionName );
2283
2291
}
2284
2292
0 commit comments