diff --git a/mongodb/java8/README.md b/mongodb/java8/README.md new file mode 100644 index 00000000..18883f71 --- /dev/null +++ b/mongodb/java8/README.md @@ -0,0 +1,21 @@ +# Spring Data MongoDB - Java 8 examples + +This project contains samples of Java 8 specific features of Spring Data (MongoDB). + +## Support for JDK 8's `Stream` for repository methods + +Repository methods can use a Java 8 `Stream` as a return type which will cause the reading of the results and the to-object-conversion of the `DBObject` to happen while iterating over the stream. + +```java +public interface PersonRepository extends CrudRepository { + + @Override + List findAll(); + + //Custom Query method returning a Java 8 Stream + @Query("{}") + Stream findAllByCustomQueryWithStream(); +} +``` + +The test cases in `PersonRepositoryIntegrationTest` oppose a plain `List` based query method with one that uses a `Stream` and shows how the former pulls all data into memory first and the iteration is done over the pre-populated list. The execution of the `Stream`-based method in contrast shows that the individual elements are read and converted while iterating the stream. diff --git a/mongodb/java8/pom.xml b/mongodb/java8/pom.xml new file mode 100644 index 00000000..8a3cfbc9 --- /dev/null +++ b/mongodb/java8/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + + + org.springframework.data.examples + spring-data-mongodb-examples + 1.0.0.BUILD-SNAPSHOT + + + spring-data-mongodb-java8 + Spring Data MongoDB - Java 8 specific features + + + Fowler-BUILD-SNAPSHOT + + + + + ${project.groupId} + spring-data-mongodb-utils + ${project.version} + test + + + + org.springframework.data + spring-data-mongodb + + + + + \ No newline at end of file diff --git a/mongodb/java8/src/main/java/example/springdata/mongodb/people/ApplicationConfiguration.java b/mongodb/java8/src/main/java/example/springdata/mongodb/people/ApplicationConfiguration.java new file mode 100644 index 00000000..259e2a49 --- /dev/null +++ b/mongodb/java8/src/main/java/example/springdata/mongodb/people/ApplicationConfiguration.java @@ -0,0 +1,34 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener; + +/** + * Simple configuration that registers a {@link LoggingEventListener} to demonstrate mapping behaviour when Java 8 + * Streams are used. + * + * @author Thomas Darimont + */ +@SpringBootApplication +class ApplicationConfiguration { + + public @Bean LoggingEventListener mongoEventListener() { + return new LoggingEventListener(); + } +} diff --git a/mongodb/java8/src/main/java/example/springdata/mongodb/people/Person.java b/mongodb/java8/src/main/java/example/springdata/mongodb/people/Person.java new file mode 100644 index 00000000..65003db0 --- /dev/null +++ b/mongodb/java8/src/main/java/example/springdata/mongodb/people/Person.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import org.springframework.data.annotation.Id; + +/** + * An entity to represent a Person. + * + * @author Thomas Darimont + */ +@Data +@RequiredArgsConstructor +public class Person { + + private @Id String id; + private final String firstname; + private final String lastname; +} diff --git a/mongodb/java8/src/main/java/example/springdata/mongodb/people/PersonRepository.java b/mongodb/java8/src/main/java/example/springdata/mongodb/people/PersonRepository.java new file mode 100644 index 00000000..d1399c64 --- /dev/null +++ b/mongodb/java8/src/main/java/example/springdata/mongodb/people/PersonRepository.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people; + +import java.util.List; +import java.util.stream.Stream; + +import org.springframework.data.mongodb.repository.Query; +import org.springframework.data.repository.CrudRepository; + +/** + * Repository interface to manage {@link Person} instances. + * + * @author Thomas Darimont + */ +public interface PersonRepository extends CrudRepository { + + List findAll(); + + @Query("{}") + Stream findAllByCustomQueryWithStream(); +} diff --git a/mongodb/java8/src/test/java/example/springdata/mongodb/people/PersonRepositoryIntegrationTest.java b/mongodb/java8/src/test/java/example/springdata/mongodb/people/PersonRepositoryIntegrationTest.java new file mode 100644 index 00000000..348b9bf6 --- /dev/null +++ b/mongodb/java8/src/test/java/example/springdata/mongodb/people/PersonRepositoryIntegrationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people; + +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import example.springdata.mongodb.util.RequiresMongoDB; + +/** + * Integration test for {@link PersonRepository}. + * + * @author Thomas Darimont + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = ApplicationConfiguration.class) +public class PersonRepositoryIntegrationTest { + + @ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion(); + + @Autowired PersonRepository repository; + @Autowired MongoOperations operations; + + Person dave, oliver, carter; + + @Before + public void setUp() { + + repository.deleteAll(); + + dave = repository.save(new Person("Dave", "Matthews")); + oliver = repository.save(new Person("Oliver August", "Matthews")); + carter = repository.save(new Person("Carter", "Beauford")); + } + + /** + * Note that the all object conversions are preformed before the results are printed to the console. + */ + @Test + public void shouldPerformConversionBeforeResultProcessing() { + repository.findAll().forEach(System.out::println); + } + + /** + * Note that the object conversions are preformed during stream processing as one can see from the + * {@link LoggingEventListener} output that is printed to the console. + */ + @Test + public void shouldPerformConversionDuringJava8StreamProcessing() { + repository.findAllByCustomQueryWithStream().forEach(System.out::println); + } +} diff --git a/mongodb/java8/src/test/java/example/springdata/mongodb/people/package-info.java b/mongodb/java8/src/test/java/example/springdata/mongodb/people/package-info.java new file mode 100644 index 00000000..e4c4d5e2 --- /dev/null +++ b/mongodb/java8/src/test/java/example/springdata/mongodb/people/package-info.java @@ -0,0 +1,5 @@ +/** + * Package showing usage of Spring Data MongoDB Repositories with Java 8. + */ +package example.springdata.mongodb.people; + diff --git a/mongodb/pom.xml b/mongodb/pom.xml index dc079d68..e0900d42 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -14,12 +14,13 @@ Spring Data MongoDB - Examples Sample projects for Spring Data MongoDB http://projects.spring.io/spring-data-mongodb - 2011-2014 + 2011-2015 aggregation example text-search + java8 util