20
20
21
21
import java .lang .reflect .InvocationTargetException ;
22
22
import java .util .ArrayList ;
23
+ import java .util .Collection ;
23
24
import java .util .Collections ;
24
25
import java .util .HashMap ;
26
+ import java .util .Iterator ;
25
27
import java .util .List ;
26
28
import java .util .Map ;
27
29
import java .util .Set ;
@@ -88,6 +90,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
88
90
private static final Log LOGGER = LogFactory .getLog (MongoTemplate .class );
89
91
90
92
private static final String ID = "_id" ;
93
+ private static final List <String > ITERABLE_CLASSES = new ArrayList <String >() {{
94
+ add (List .class .getName ());
95
+ add (Collection .class .getName ());
96
+ add (Iterator .class .getName ());
97
+ }};
91
98
92
99
/*
93
100
* WriteConcern to be used for write operations if it has been specified. Otherwise
@@ -373,7 +380,7 @@ public void dropCollection(String collectionName) {
373
380
public Void doInCollection (DBCollection collection ) throws MongoException , DataAccessException {
374
381
collection .drop ();
375
382
if (LOGGER .isDebugEnabled ()) {
376
- LOGGER .debug ("Dropped collection [" + collection .getFullName () + "]" );
383
+ LOGGER .debug ("Dropped collection [" + collection .getFullName () + "]" );
377
384
}
378
385
return null ;
379
386
}
@@ -475,16 +482,27 @@ public <T> T findAndRemove(String collectionName, Query query, Class<T> targetCl
475
482
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
476
483
*/
477
484
public void insert (Object objectToSave ) {
485
+ ensureNotIterable (objectToSave );
478
486
insert (determineEntityCollectionName (objectToSave ), objectToSave );
479
487
}
480
488
481
489
/* (non-Javadoc)
482
490
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, java.lang.Object)
483
491
*/
484
492
public void insert (String collectionName , Object objectToSave ) {
493
+ ensureNotIterable (objectToSave );
485
494
doInsert (collectionName , objectToSave , this .mongoConverter );
486
495
}
487
496
497
+ protected void ensureNotIterable (Object o ) {
498
+ if (null != o ) {
499
+ if (o .getClass ().isArray () ||
500
+ ITERABLE_CLASSES .contains (o .getClass ().getName ())) {
501
+ throw new IllegalArgumentException ("Cannot use a collection here." );
502
+ }
503
+ }
504
+ }
505
+
488
506
/**
489
507
* Prepare the collection before any processing is done using it. This allows a convenient way to apply
490
508
* settings like slaveOk() etc. Can be overridden in sub-classes.
@@ -613,21 +631,6 @@ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T>
613
631
}
614
632
615
633
protected Object insertDBObject (String collectionName , final DBObject dbDoc ) {
616
-
617
- // DATADOC-95: This will prevent null objects from being saved.
618
- // if (dbDoc.keySet().isEmpty()) {
619
- // return null;
620
- // }
621
-
622
- // TODO: Need to move this to more central place
623
- if (dbDoc .containsField ("_id" )) {
624
- if (dbDoc .get ("_id" ) instanceof String ) {
625
- ObjectId oid = convertIdValue (this .mongoConverter , dbDoc .get ("_id" ));
626
- if (oid != null ) {
627
- dbDoc .put ("_id" , oid );
628
- }
629
- }
630
- }
631
634
if (LOGGER .isDebugEnabled ()) {
632
635
LOGGER .debug ("insert DBObject containing fields: " + dbDoc .keySet () + " in collection: " + collectionName );
633
636
}
@@ -645,22 +648,10 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
645
648
}
646
649
647
650
protected List <ObjectId > insertDBObjectList (String collectionName , final List <DBObject > dbDocList ) {
648
-
649
651
if (dbDocList .isEmpty ()) {
650
652
return Collections .emptyList ();
651
653
}
652
654
653
- // TODO: Need to move this to more central place
654
- for (DBObject dbDoc : dbDocList ) {
655
- if (dbDoc .containsField ("_id" )) {
656
- if (dbDoc .get ("_id" ) instanceof String ) {
657
- ObjectId oid = convertIdValue (this .mongoConverter , dbDoc .get ("_id" ));
658
- if (oid != null ) {
659
- dbDoc .put ("_id" , oid );
660
- }
661
- }
662
- }
663
- }
664
655
if (LOGGER .isDebugEnabled ()) {
665
656
LOGGER .debug ("insert list of DBObjects containing " + dbDocList .size () + " items" );
666
657
}
@@ -690,20 +681,6 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
690
681
}
691
682
692
683
protected Object saveDBObject (String collectionName , final DBObject dbDoc ) {
693
-
694
- if (dbDoc .keySet ().isEmpty ()) {
695
- return null ;
696
- }
697
-
698
- // TODO: Need to move this to more central place
699
- if (dbDoc .containsField ("_id" )) {
700
- if (dbDoc .get ("_id" ) instanceof String ) {
701
- ObjectId oid = convertIdValue (this .mongoConverter , dbDoc .get ("_id" ));
702
- if (oid != null ) {
703
- dbDoc .put ("_id" , oid );
704
- }
705
- }
706
- }
707
684
if (LOGGER .isDebugEnabled ()) {
708
685
LOGGER .debug ("save DBObject containing fields: " + dbDoc .keySet ());
709
686
}
@@ -824,9 +801,9 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
824
801
DBObject dboq = mapper .getMappedObject (queryObject , entity );
825
802
WriteResult wr = null ;
826
803
WriteConcern writeConcernToUse = prepareWriteConcern (writeConcern );
827
- if (LOGGER .isDebugEnabled ()) {
828
- LOGGER .debug ("remove using query: " + queryObject + " in collection: " + collection .getName ());
829
- }
804
+ if (LOGGER .isDebugEnabled ()) {
805
+ LOGGER .debug ("remove using query: " + queryObject + " in collection: " + collection .getName ());
806
+ }
830
807
if (writeConcernToUse == null ) {
831
808
wr = collection .remove (dboq );
832
809
} else {
@@ -889,7 +866,7 @@ public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
889
866
DBCollection coll = db .createCollection (collectionName , collectionOptions );
890
867
// TODO: Emit a collection created event
891
868
if (LOGGER .isDebugEnabled ()) {
892
- LOGGER .debug ("Created collection [" + coll .getFullName () + "]" );
869
+ LOGGER .debug ("Created collection [" + coll .getFullName () + "]" );
893
870
}
894
871
return coll ;
895
872
}
@@ -1014,7 +991,7 @@ protected Object getIdValue(Object object) {
1014
991
if (idProp == null ) {
1015
992
throw new MappingException ("No id property found for object of type " + entity .getType ().getName ());
1016
993
}
1017
-
994
+
1018
995
ConversionService service = mongoConverter .getConversionService ();
1019
996
1020
997
try {
0 commit comments