Browse Source

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.
pull/322/merge
Christoph Strobl 10 years ago committed by Oliver Gierke
parent
commit
b31efb46ec
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

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

@ -264,7 +264,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
accessor.setProperty(idProperty, idValue); accessor.setProperty(idProperty, idValue);
} }
final ObjectPath currentPath = path.push(result, entity, idValue); final ObjectPath currentPath = path.push(result, entity, idValue != null ? dbo.get(idProperty.getFieldName())
: null);
// Set properties not already set in the constructor // Set properties not already set in the constructor
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() { entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {

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

@ -3035,7 +3035,7 @@ public class MongoTemplateTests {
*/ */
@Test @Test
public void takesLimitIntoAccountWhenStreaming() { public void takesLimitIntoAccountWhenStreaming() {
Person youngestPerson = new Person("John", 20); Person youngestPerson = new Person("John", 20);
Person oldestPerson = new Person("Jane", 42); Person oldestPerson = new Person("Jane", 42);
@ -3049,6 +3049,31 @@ public class MongoTemplateTests {
assertThat(stream.hasNext(), is(false)); assertThat(stream.hasNext(), is(false));
} }
/**
* @see DATAMONGO-1204
*/
@Test
public void resolvesCyclicDBRefCorrectly() {
SomeMessage message = new SomeMessage();
SomeContent content = new SomeContent();
template.save(message);
template.save(content);
message.dbrefContent = content;
content.dbrefMessage = message;
template.save(message);
template.save(content);
SomeMessage messageLoaded = template.findOne(query(where("id").is(message.id)), SomeMessage.class);
SomeContent contentLoaded = template.findOne(query(where("id").is(content.id)), SomeContent.class);
assertThat(messageLoaded.dbrefContent.id, is(contentLoaded.id));
assertThat(contentLoaded.dbrefMessage.id, is(messageLoaded.id));
}
static class DoucmentWithNamedIdField { static class DoucmentWithNamedIdField {
@Id String someIdKey; @Id String someIdKey;
@ -3351,6 +3376,7 @@ public class MongoTemplateTests {
String id; String id;
String text; String text;
String name; String name;
@org.springframework.data.mongodb.core.mapping.DBRef SomeMessage dbrefMessage;
public String getName() { public String getName() {
return name; return name;

Loading…
Cancel
Save