Skip to content

Commit 43d0f74

Browse files
committed
Initial stab at changing the way IDs are handled in the mapping converter.
1 parent aeea1bc commit 43d0f74

File tree

10 files changed

+140
-149
lines changed

10 files changed

+140
-149
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoCollectionUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
/**
2121
* Helper class featuring helper methods for working with MongoDb collections.
2222
* <p/>
23-
* <p>
23+
* <p/>
2424
* Mainly intended for internal use within the framework.
25-
*
25+
*
2626
* @author Thomas Risberg
2727
* @since 1.0
2828
*/
@@ -37,13 +37,14 @@ private MongoCollectionUtils() {
3737

3838
/**
3939
* Obtains the collection name to use for the provided class
40-
*
41-
* @param entityClass
42-
* The class to determine the preferred collection name for
40+
*
41+
* @param entityClass The class to determine the preferred collection name for
4342
* @return The preferred collection name
4443
*/
4544
public static String getPreferredCollectionName(Class<?> entityClass) {
46-
return entityClass.getSimpleName();
45+
String name = entityClass.getSimpleName();
46+
char firstChar = name.charAt(0);
47+
return (String.valueOf(firstChar).toLowerCase() + name.substring(1));
4748
}
4849

4950
}

spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import java.lang.reflect.InvocationTargetException;
2222
import java.util.ArrayList;
23+
import java.util.Collection;
2324
import java.util.Collections;
2425
import java.util.HashMap;
26+
import java.util.Iterator;
2527
import java.util.List;
2628
import java.util.Map;
2729
import java.util.Set;
@@ -88,6 +90,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
8890
private static final Log LOGGER = LogFactory.getLog(MongoTemplate.class);
8991

9092
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+
}};
9198

9299
/*
93100
* WriteConcern to be used for write operations if it has been specified. Otherwise
@@ -373,7 +380,7 @@ public void dropCollection(String collectionName) {
373380
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
374381
collection.drop();
375382
if (LOGGER.isDebugEnabled()) {
376-
LOGGER.debug("Dropped collection ["+ collection.getFullName() + "]");
383+
LOGGER.debug("Dropped collection [" + collection.getFullName() + "]");
377384
}
378385
return null;
379386
}
@@ -475,16 +482,27 @@ public <T> T findAndRemove(String collectionName, Query query, Class<T> targetCl
475482
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
476483
*/
477484
public void insert(Object objectToSave) {
485+
ensureNotIterable(objectToSave);
478486
insert(determineEntityCollectionName(objectToSave), objectToSave);
479487
}
480488

481489
/* (non-Javadoc)
482490
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.String, java.lang.Object)
483491
*/
484492
public void insert(String collectionName, Object objectToSave) {
493+
ensureNotIterable(objectToSave);
485494
doInsert(collectionName, objectToSave, this.mongoConverter);
486495
}
487496

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+
488506
/**
489507
* Prepare the collection before any processing is done using it. This allows a convenient way to apply
490508
* settings like slaveOk() etc. Can be overridden in sub-classes.
@@ -613,21 +631,6 @@ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T>
613631
}
614632

615633
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-
}
631634
if (LOGGER.isDebugEnabled()) {
632635
LOGGER.debug("insert DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
633636
}
@@ -645,22 +648,10 @@ public Object doInCollection(DBCollection collection) throws MongoException, Dat
645648
}
646649

647650
protected List<ObjectId> insertDBObjectList(String collectionName, final List<DBObject> dbDocList) {
648-
649651
if (dbDocList.isEmpty()) {
650652
return Collections.emptyList();
651653
}
652654

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-
}
664655
if (LOGGER.isDebugEnabled()) {
665656
LOGGER.debug("insert list of DBObjects containing " + dbDocList.size() + " items");
666657
}
@@ -690,20 +681,6 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
690681
}
691682

692683
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-
}
707684
if (LOGGER.isDebugEnabled()) {
708685
LOGGER.debug("save DBObject containing fields: " + dbDoc.keySet());
709686
}
@@ -824,9 +801,9 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
824801
DBObject dboq = mapper.getMappedObject(queryObject, entity);
825802
WriteResult wr = null;
826803
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+
}
830807
if (writeConcernToUse == null) {
831808
wr = collection.remove(dboq);
832809
} else {
@@ -889,7 +866,7 @@ public DBCollection doInDB(DB db) throws MongoException, DataAccessException {
889866
DBCollection coll = db.createCollection(collectionName, collectionOptions);
890867
// TODO: Emit a collection created event
891868
if (LOGGER.isDebugEnabled()) {
892-
LOGGER.debug("Created collection [" + coll.getFullName() + "]");
869+
LOGGER.debug("Created collection [" + coll.getFullName() + "]");
893870
}
894871
return coll;
895872
}
@@ -1014,7 +991,7 @@ protected Object getIdValue(Object object) {
1014991
if (idProp == null) {
1015992
throw new MappingException("No id property found for object of type " + entity.getType().getName());
1016993
}
1017-
994+
1018995
ConversionService service = mongoConverter.getConversionService();
1019996

1020997
try {

0 commit comments

Comments
 (0)