Skip to content

DATAMONGO-1287 - Fix double fetching for lazy DbRef used in ctor. #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.9.0.BUILD-SNAPSHOT</version>
<version>1.9.0.DATAMONGO-1287-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ public void doWithAssociation(Association<MongoPersistentProperty> association)
final MongoPersistentProperty property = association.getInverse();
Object value = dbo.get(property.getFieldName());

if (value == null) {
if (value == null
|| (entity.getPersistenceConstructor().hasParameters() && entity.isConstructorArgument(property) && accessor
.getProperty(property) != null)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ protected void cleanDb() {
template.dropCollection(SomeTemplate.class);
template.dropCollection(Address.class);
template.dropCollection(DocumentWithCollectionOfSamples.class);
template.dropCollection(DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
}

@Test
Expand Down Expand Up @@ -3035,7 +3036,7 @@ public void takesSortIntoAccountWhenStreaming() {
*/
@Test
public void takesLimitIntoAccountWhenStreaming() {

Person youngestPerson = new Person("John", 20);
Person oldestPerson = new Person("Jane", 42);

Expand All @@ -3049,6 +3050,75 @@ public void takesLimitIntoAccountWhenStreaming() {
assertThat(stream.hasNext(), is(false));
}

/**
* @see DATAMONGO-1287
*/
@Test
public void shouldReuseAlreadyResolvedLazyLoadedDBRefWhenUsedAsPersistenceConstrcutorArgument() {

Document docInCtor = new Document();
docInCtor.id = "doc-in-ctor";
template.save(docInCtor);

DocumentWithLazyDBrefUsedInPresistenceConstructor source = new DocumentWithLazyDBrefUsedInPresistenceConstructor(
docInCtor);

template.save(source);

DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template.findOne(query(where("id").is(source.id)),
DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
assertThat(loaded.refToDocUsedInCtor, not(instanceOf(LazyLoadingProxy.class)));
assertThat(loaded.refToDocNotUsedInCtor, nullValue());
}

/**
* @see DATAMONGO-1287
*/
@Test
public void shouldNotReuseLazyLoadedDBRefWhenTypeUsedInPersistenceConstrcutorButValueRefersToAnotherProperty() {

Document docNotUsedInCtor = new Document();
docNotUsedInCtor.id = "doc-but-not-used-in-ctor";
template.save(docNotUsedInCtor);

DocumentWithLazyDBrefUsedInPresistenceConstructor source = new DocumentWithLazyDBrefUsedInPresistenceConstructor(
null);
source.refToDocNotUsedInCtor = docNotUsedInCtor;

template.save(source);

DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template.findOne(query(where("id").is(source.id)),
DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class));
assertThat(loaded.refToDocUsedInCtor, nullValue());
}

/**
* @see DATAMONGO-1287
*/
@Test
public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedInPersistenceConstrcutor() {

Document docInCtor = new Document();
docInCtor.id = "doc-in-ctor";
template.save(docInCtor);

Document docNotUsedInCtor = new Document();
docNotUsedInCtor.id = "doc-but-not-used-in-ctor";
template.save(docNotUsedInCtor);

DocumentWithLazyDBrefUsedInPresistenceConstructor source = new DocumentWithLazyDBrefUsedInPresistenceConstructor(
docInCtor);
source.refToDocNotUsedInCtor = docNotUsedInCtor;

template.save(source);

DocumentWithLazyDBrefUsedInPresistenceConstructor loaded = template.findOne(query(where("id").is(source.id)),
DocumentWithLazyDBrefUsedInPresistenceConstructor.class);
assertThat(loaded.refToDocUsedInCtor, not(instanceOf(LazyLoadingProxy.class)));
assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class));
}

static class DoucmentWithNamedIdField {

@Id String someIdKey;
Expand Down Expand Up @@ -3375,4 +3445,18 @@ static class SomeMessage {
@org.springframework.data.mongodb.core.mapping.DBRef SomeContent dbrefContent;
SomeContent normalContent;
}

static class DocumentWithLazyDBrefUsedInPresistenceConstructor {

@Id String id;
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) Document refToDocUsedInCtor;
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) Document refToDocNotUsedInCtor;

@PersistenceConstructor
public DocumentWithLazyDBrefUsedInPresistenceConstructor(Document refToDocUsedInCtor) {
this.refToDocUsedInCtor = refToDocUsedInCtor;
}

}

}