Skip to content

Commit 0468ba3

Browse files
author
Thomas Risberg
committed
DATADOC-48 adding checks not to overwrite existing keys in changeset
1 parent 95510e4 commit 0468ba3

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

spring-data-mongodb-cross-store/src/main/java/org/springframework/data/persistence/document/mongo/MongoChangeSetPersister.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.mongodb.MongoException;
77
import org.apache.commons.logging.Log;
88
import org.apache.commons.logging.LogFactory;
9-
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.dao.DataAccessException;
1110
import org.springframework.dao.DataIntegrityViolationException;
1211
import org.springframework.data.document.mongodb.CollectionCallback;
@@ -43,27 +42,32 @@ public void getPersistentState(Class<? extends ChangeSetBacked> entityClass,
4342
final DBObject dbk = new BasicDBObject();
4443
dbk.put(ENTITY_ID, id);
4544
dbk.put(ENTITY_CLASS, entityClass.getName());
45+
if (log.isDebugEnabled()) {
46+
log.debug("Loading MongoDB data for " + dbk);
47+
}
4648
mongoTemplate.execute(collName, new CollectionCallback<Object>() {
4749
@Override
4850
public Object doInCollection(DBCollection collection)
4951
throws MongoException, DataAccessException {
5052
for (DBObject dbo : collection.find(dbk)) {
5153
String key = (String) dbo.get(ENTITY_FIELD_NAME);
52-
String className = (String) dbo.get(ENTITY_FIELD_CLASS);
53-
if (className == null) {
54-
throw new DataIntegrityViolationException(
55-
"Unble to convert property " + key
56-
+ ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
54+
if (log.isDebugEnabled()) {
55+
log.debug("Processing key: " + key);
5756
}
58-
Class<?> clazz = null;
59-
try {
60-
clazz = Class.forName(className);
61-
} catch (ClassNotFoundException e) {
62-
throw new DataIntegrityViolationException(
63-
"Unble to convert property " + key + " of type " + className, e);
57+
if (!changeSet.getValues().containsKey(key)) {
58+
String className = (String) dbo.get(ENTITY_FIELD_CLASS);
59+
if (className == null) {
60+
throw new DataIntegrityViolationException(
61+
"Unble to convert property " + key
62+
+ ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available");
63+
}
64+
Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
65+
Object value = mongoTemplate.getConverter().read(clazz, dbo);
66+
if (log.isDebugEnabled()) {
67+
log.debug("Adding to ChangeSet: " + key);
68+
}
69+
changeSet.set(key, value);
6470
}
65-
Object value = mongoTemplate.getConverter().read(clazz, dbo);
66-
changeSet.set(key, value);
6771
}
6872
return null;
6973
}

0 commit comments

Comments
 (0)