Skip to content

Commit b31efb4

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1204 - ObjectPath now uses raw id values to track resolved objects.
We now use the native id within ObjectPath for checking if a DBref has already been resolved. This is required as MongoDB Java driver 3 generation changed ObjectId.equals(…) which now performs a type check. Original pull request: #334. Related pull request: #288.
1 parent ef34770 commit b31efb4

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
264264
accessor.setProperty(idProperty, idValue);
265265
}
266266

267-
final ObjectPath currentPath = path.push(result, entity, idValue);
267+
final ObjectPath currentPath = path.push(result, entity, idValue != null ? dbo.get(idProperty.getFieldName())
268+
: null);
268269

269270
// Set properties not already set in the constructor
270271
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -3035,7 +3035,7 @@ public void takesSortIntoAccountWhenStreaming() {
30353035
*/
30363036
@Test
30373037
public void takesLimitIntoAccountWhenStreaming() {
3038-
3038+
30393039
Person youngestPerson = new Person("John", 20);
30403040
Person oldestPerson = new Person("Jane", 42);
30413041

@@ -3049,6 +3049,31 @@ public void takesLimitIntoAccountWhenStreaming() {
30493049
assertThat(stream.hasNext(), is(false));
30503050
}
30513051

3052+
/**
3053+
* @see DATAMONGO-1204
3054+
*/
3055+
@Test
3056+
public void resolvesCyclicDBRefCorrectly() {
3057+
3058+
SomeMessage message = new SomeMessage();
3059+
SomeContent content = new SomeContent();
3060+
3061+
template.save(message);
3062+
template.save(content);
3063+
3064+
message.dbrefContent = content;
3065+
content.dbrefMessage = message;
3066+
3067+
template.save(message);
3068+
template.save(content);
3069+
3070+
SomeMessage messageLoaded = template.findOne(query(where("id").is(message.id)), SomeMessage.class);
3071+
SomeContent contentLoaded = template.findOne(query(where("id").is(content.id)), SomeContent.class);
3072+
3073+
assertThat(messageLoaded.dbrefContent.id, is(contentLoaded.id));
3074+
assertThat(contentLoaded.dbrefMessage.id, is(messageLoaded.id));
3075+
}
3076+
30523077
static class DoucmentWithNamedIdField {
30533078

30543079
@Id String someIdKey;
@@ -3351,6 +3376,7 @@ public static class SomeContent {
33513376
String id;
33523377
String text;
33533378
String name;
3379+
@org.springframework.data.mongodb.core.mapping.DBRef SomeMessage dbrefMessage;
33543380

33553381
public String getName() {
33563382
return name;

0 commit comments

Comments
 (0)