diff --git a/mongodb/example/README.md b/mongodb/example/README.md new file mode 100644 index 00000000..cd4c9e44 --- /dev/null +++ b/mongodb/example/README.md @@ -0,0 +1,9 @@ +# Spring Data MongoDB - Example + +This project contains examples for: + +* Derived finder and geo spatial queries using a `Repository` interface. +* `EntityCallback` API usage for before convert/save interaction. +* Result projections for DTOs and interface types. +* Query metadata. + diff --git a/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ApplicationConfiguration.java b/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ApplicationConfiguration.java index 10357fda..9e2c5239 100644 --- a/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ApplicationConfiguration.java +++ b/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ApplicationConfiguration.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -24,16 +24,19 @@ import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback import com.mongodb.MongoClient; /** - * Test configuration to connect to a MongoDB named "test" and using a {@link MongoClient}. Also enables Spring Data - * repositories for MongoDB. + * Test configuration to connect to a MongoDB named "test" using a {@link MongoClient}.
+ * Also enables Spring Data repositories for MongoDB. * * @author Mark Paluch + * @author Christoph Strobl */ @SpringBootApplication class ApplicationConfiguration { /** - * Callback to update {@link ImmutablePerson}. + * Register the {@link BeforeConvertCallback} used to update an {@link ImmutablePerson} before handing over the newly + * created instance to the actual mapping layer performing the conversion into the store native + * {@link org.bson.Document} representation. * * @return a {@link BeforeConvertCallback} for {@link ImmutablePerson}. */ @@ -44,6 +47,7 @@ class ApplicationConfiguration { int randomNumber = ThreadLocalRandom.current().nextInt(1, 100); + // withRandomNumber is a so called wither method returning a new instance of the entity with a new value assigned return immutablePerson.withRandomNumber(randomNumber); }; } diff --git a/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ImmutablePerson.java b/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ImmutablePerson.java index 6fd06e06..338ad07c 100644 --- a/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ImmutablePerson.java +++ b/mongodb/example/src/main/java/example/springdata/mongodb/immutable/ImmutablePerson.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,7 +32,6 @@ import org.bson.types.ObjectId; public class ImmutablePerson { private final ObjectId id; - private final int randomNumber; public ImmutablePerson() { diff --git a/mongodb/example/src/test/java/example/springdata/mongodb/immutable/ImmutableEntityIntegrationTest.java b/mongodb/example/src/test/java/example/springdata/mongodb/immutable/ImmutableEntityIntegrationTest.java index ef59bf16..950dcaea 100644 --- a/mongodb/example/src/test/java/example/springdata/mongodb/immutable/ImmutableEntityIntegrationTest.java +++ b/mongodb/example/src/test/java/example/springdata/mongodb/immutable/ImmutableEntityIntegrationTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoOperations; @@ -30,6 +29,7 @@ import org.springframework.test.context.junit4.SpringRunner; * Integration test for {@link ImmutablePerson} showing features around immutable object support. * * @author Mark Paluch + * @author Christoph Strobl */ @RunWith(SpringRunner.class) @SpringBootTest @@ -39,8 +39,7 @@ public class ImmutableEntityIntegrationTest { @Before public void setUp() { - - operations.remove(ImmutablePerson.class); + operations.dropCollection(ImmutablePerson.class); } /**