diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java index 61f624fc1..76c066e10 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ObjectPath.java @@ -17,8 +17,10 @@ package org.springframework.data.mongodb.core.convert; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; +import org.springframework.data.util.Lazy; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -46,14 +48,14 @@ class ObjectPath { private final @Nullable ObjectPath parent; private final @Nullable Object object; private final @Nullable Object idValue; - private final String collection; + private final Lazy collection; private ObjectPath() { this.parent = null; this.object = null; this.idValue = null; - this.collection = ""; + this.collection = Lazy.empty(); } /** @@ -64,7 +66,7 @@ class ObjectPath { * @param idValue * @param collection */ - private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, String collection) { + private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, Lazy collection) { this.parent = parent; this.object = object; @@ -85,7 +87,7 @@ class ObjectPath { Assert.notNull(object, "Object must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); - return new ObjectPath(this, object, id, entity.getCollection()); + return new ObjectPath(this, object, id, Lazy.of(() -> entity.getCollection())); } /** @@ -175,7 +177,7 @@ class ObjectPath { } private String getCollection() { - return collection; + return collection.get(); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java index a0a8084bd..84a9c1040 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/ObjectPathUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.data.mongodb.core.convert; import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; import org.junit.Before; import org.junit.Test; @@ -81,6 +82,19 @@ public class ObjectPathUnitTests { assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull(); } + @Test // DATAMONGO-2267 + public void collectionLookupShouldBeLazy/* because we may need to resolve SpEL which can be pretty expensive */() { + + MongoPersistentEntity spied = spy(one); + ObjectPath path = ObjectPath.ROOT.push(new EntityThree(), spied, "id-1"); + + verify(spied, never()).getCollection(); + + path.getPathItem("id-1", "foo", EntityTwo.class); + + verify(spied).getCollection(); + } + @Document("one") static class EntityOne {