Skip to content

Commit 8c50baa

Browse files
DATAMONGO-1509 - Hacking.
Always add type hint as last property of document.
1 parent d6f560d commit 8c50baa

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,19 @@ public void write(final Object obj, final DBObject dbo) {
348348
return;
349349
}
350350

351-
Class<?> entityType = obj.getClass();
351+
Class<?> entityType = ClassUtils.getUserClass(obj.getClass());
352+
352353
boolean handledByCustomConverter = conversions.getCustomWriteTarget(entityType, DBObject.class) != null;
353354
TypeInformation<? extends Object> type = ClassTypeInformation.from(entityType);
354355

355-
if (!handledByCustomConverter && !(dbo instanceof BasicDBList)) {
356-
typeMapper.writeType(type, dbo);
357-
}
358356

359357
Object target = obj instanceof LazyLoadingProxy ? ((LazyLoadingProxy) obj).getTarget() : obj;
360358

361359
writeInternal(target, dbo, type);
360+
361+
if (!handledByCustomConverter && !(dbo instanceof BasicDBList)) {
362+
typeMapper.writeType(type, dbo);
363+
}
362364
}
363365

364366
/**
@@ -516,12 +518,13 @@ protected void writePropertyInternal(Object obj, DBObject dbo, MongoPersistentPr
516518
Object existingValue = accessor.get(prop);
517519
BasicDBObject propDbObj = existingValue instanceof BasicDBObject ? (BasicDBObject) existingValue
518520
: new BasicDBObject();
519-
addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, propDbObj);
521+
520522

521523
MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
522524
? mappingContext.getPersistentEntity(obj.getClass()) : mappingContext.getPersistentEntity(type);
523525

524526
writeInternal(obj, propDbObj, entity);
527+
addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, propDbObj);
525528
accessor.put(prop, propDbObj);
526529
}
527530

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.joda.time.DateTime;
4444
import org.junit.After;
4545
import org.junit.Before;
46+
import org.junit.Ignore;
4647
import org.junit.Rule;
4748
import org.junit.Test;
4849
import org.junit.rules.ExpectedException;
@@ -3470,6 +3471,22 @@ public void onBeforeSave(BeforeSaveEvent<Document> event) {
34703471
assertThat(document.id, is(notNullValue()));
34713472
}
34723473

3474+
3475+
/**
3476+
* @see DATAMONGO-1509
3477+
*/
3478+
@Test
3479+
public void findsByGnericNestedListElements() {
3480+
3481+
List<Model> modelList = Arrays.<Model>asList(new ModelA("value"));
3482+
DocumentWithCollection dwc = new DocumentWithCollection(modelList);
3483+
3484+
template.insert(dwc);
3485+
3486+
Query query = query(where("models").is(modelList));
3487+
assertThat(template.findOne(query, DocumentWithCollection.class), is(equalTo(dwc)));
3488+
}
3489+
34733490
static class TypeWithNumbers {
34743491

34753492
@Id String id;
@@ -3549,6 +3566,7 @@ static class DocumentWithDBRefCollection {
35493566
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) public Map<String, Sample> lazyDbRefAnnotatedMap;
35503567
}
35513568

3569+
@EqualsAndHashCode
35523570
static class DocumentWithCollection {
35533571

35543572
@Id String id;
@@ -3601,6 +3619,7 @@ static interface Model {
36013619
String id();
36023620
}
36033621

3622+
@EqualsAndHashCode
36043623
static class ModelA implements Model {
36053624

36063625
@Id String id;
@@ -3835,7 +3854,7 @@ static class WithGeoJson {
38353854

38363855
@Id String id;
38373856
@Version //
3838-
Integer version;
3857+
Integer version;
38393858
String description;
38403859
GeoJsonPoint point;
38413860
}

0 commit comments

Comments
 (0)