Merge Java 8 samples into basic one.

This commit is contained in:
Mark Paluch
2021-04-28 10:57:59 +02:00
parent 20dfd398c8
commit b44dc3dea1
24 changed files with 44 additions and 286 deletions

View File

@@ -9,7 +9,6 @@ We have separate folders for the samples of individual modules:
## Spring Data for Apache Cassandra
* `example` - Shows core Spring Data support for Apache Cassandra.
* `java8` - Java 8 specific functionality like the support for JSR-310 types in object mapping.
* `kotlin` - Example for using Cassandra with Kotlin.
* `reactive` - Example project to show reactive template and repository support.
@@ -57,7 +56,6 @@ We have separate folders for the samples of individual modules:
* `fluent-api` - Example project to show the new fluent API (`MongoTemplate`-alternative) to interact with MongoDB.
* `geo-json` - Example project showing usage of [GeoJSON](http://geojson.org) with MongoDB.
* `gridfs` - Example project showing usage of gridFS with MongoDB.
* `java8` - Example of how to use Spring Data MongoDB with Java 8 date time types as well as the usage of `Optional` as return type for repository methods. Note, this project requires to be build with JDK 8.
* `jmolecules` - Example of Spring Data MongoDB working with a jMolecules based domain model.
* `kotlin` - Example for using [Kotlin](https://kotlinlang.org/) with MongoDB.
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,12 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import java.time.LocalDate;
import java.time.ZoneId;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.Repository;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import java.util.Optional;
import java.util.stream.Stream;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,23 +13,8 @@
* 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
* Package showing Java 8 features.
*/
@Data
@RequiredArgsConstructor
public class Person {
private @Id String id;
private final String firstname;
private final String lastname;
}
package example.springdata.cassandra.streamoptional;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,9 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
import static org.assertj.core.api.Assertions.*;
package example.springdata.cassandra.streamoptional;
import example.springdata.cassandra.util.CassandraKeyspace;

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2021 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
*
* https://www.apache.org/licenses/LICENSE-2.0
* 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,
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.cassandra.java8;
package example.springdata.cassandra.streamoptional;
import static org.assertj.core.api.Assertions.*;
@@ -30,13 +30,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Integration test to show the usage of Java 8 features with Spring Data Cassandra.
* Integration test to show the usage of Java {@link Stream} and {@link Optional} features with Spring Data Cassandra.
*
* @author Mark Paluch
*/
@CassandraKeyspace
@SpringBootTest(classes = CassandraConfiguration.class)
class Java8IntegrationTests {
class StreamOptionalIntegrationTests {
@Autowired PersonRepository repository;

View File

@@ -1,20 +0,0 @@
# Spring Data Cassandra - Java 8 examples
This project contains samples of Java 8 specific features of Spring Data (Cassandra).
## 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 rows to happen while iterating over the stream.
```java
public interface PersonRepository extends CrudRepository<Person, String> {
@Override
List<Person> findAll();
// Derived query method returning a Java 8 Stream
Stream<Person> findAll();
}
```
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.

View File

@@ -1,26 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-cassandra-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-data-cassandra-java8</artifactId>
<name>Spring Data Cassandra - Java 8 specific features</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-cassandra-example-utils</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,4 +0,0 @@
/**
* Package showing Java 8 features.
*/
package example.springdata.cassandra.java8;

View File

@@ -1,3 +0,0 @@
spring.data.cassandra.keyspace-name=example
spring.data.cassandra.schema-action=recreate
spring.data.cassandra.local-datacenter=datacenter1

View File

@@ -19,7 +19,6 @@
<modules>
<module>util</module>
<module>example</module>
<module>java8</module>
<module>kotlin</module>
<module>reactive</module>
</modules>

View File

@@ -16,11 +16,13 @@
package example.springdata.mongodb.customer;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
@@ -47,4 +49,7 @@ public interface CustomerRepository extends CrudRepository<Customer, String> {
* @return
*/
GeoResults<Customer> findByAddressLocationNear(Point point, Distance distance);
@Query("{}")
Stream<Customer> findAllByCustomQueryWithStream();
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
@@ -82,6 +83,17 @@ public class CustomerRepositoryIntegrationTest {
assertThat(result.get(1), is(oliver));
}
/**
* Test case to show the usage of Java {@link Stream}.
*/
@Test
public void findCustomersAsStream() {
try (Stream<Customer> result = repository.findAllByCustomQueryWithStream()) {
result.forEach(System.out::println);
}
}
/**
* Test case to show the usage of the geo-spatial APIs to lookup people within a given distance of a reference point.
*/

View File

@@ -1,21 +0,0 @@
# 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<Person, String> {
@Override
List<Person> findAll();
//Custom Query method returning a Java 8 Stream
@Query("{}")
Stream<Person> 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.

View File

@@ -1,14 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-mongodb-java8</artifactId>
<name>Spring Data MongoDB - Java 8 specific features</name>
</project>

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2015-2021 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
*
* 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,
* 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();
}
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2015-2021 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
*
* 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,
* 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<Person, String> {
List<Person> findAll();
@Query("{}")
Stream<Person> findAllByCustomQueryWithStream();
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2015-2021 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
*
* 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,
* 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.stream.Stream;
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;
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test for {@link PersonRepository}.
*
* @author Thomas Darimont
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonRepositoryIntegrationTest {
@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() {
try (Stream<Person> result = repository.findAllByCustomQueryWithStream()) {
result.forEach(System.out::println);
}
}
}

View File

@@ -1,5 +0,0 @@
/**
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
*/
package example.springdata.mongodb.people;

View File

@@ -1,2 +0,0 @@
# Random port for embedded MongoDB
spring.data.mongodb.port=0

View File

@@ -23,7 +23,6 @@
<module>fluent-api</module>
<module>geo-json</module>
<module>gridfs</module>
<module>java8</module>
<module>jmolecules</module>
<module>kotlin</module>
<module>query-by-example</module>