From d9dfb81e126b1c35737dc33194616e1c2bcbea63 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 7 Oct 2015 19:50:49 +0200 Subject: [PATCH] #134 - Added example for using Optional as query method parameter. Got rid of Guava dependency in favor of JDK 8's Optional. Upgraded basic JPA example to Spring Data Hopper to get access to the newly introduced functionality. --- jpa/example/pom.xml | 13 +++---------- .../springdata/jpa/simple/SimpleUserRepository.java | 13 +++++++++---- .../jpa/simple/SimpleUserRepositoryTests.java | 7 ++++--- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/jpa/example/pom.xml b/jpa/example/pom.xml index 19764143..bf943ea7 100644 --- a/jpa/example/pom.xml +++ b/jpa/example/pom.xml @@ -13,16 +13,9 @@ Spring Data JPA - Example Small sample project showing the usage of Spring Data JPA. - - - - - com.google.guava - guava - 16.0.1 - - - + + Hopper-BUILD-SNAPSHOT + diff --git a/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java b/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java index 0cddebb3..4581666d 100644 --- a/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java +++ b/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java @@ -16,6 +16,7 @@ package example.springdata.jpa.simple; import java.util.List; +import java.util.Optional; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; @@ -23,8 +24,6 @@ import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; -import com.google.common.base.Optional; - /** * Simple repository interface for {@link User} instances. The interface is used to declare so called query methods, * methods to retrieve single entities or collections of them. @@ -44,7 +43,13 @@ public interface SimpleUserRepository extends CrudRepository { */ User findByTheUsersName(String username); - Optional findByUsername(String username); + /** + * Uses {@link Optional} as return and parameter type. + * + * @param username + * @return + */ + Optional findByUsername(Optional username); /** * Find all users with the given lastname. This method will be translated into a query by constructing it directly @@ -67,7 +72,7 @@ public interface SimpleUserRepository extends CrudRepository { /** * Returns all users with the given name as first- or lastname. This makes the query to method relation much more - * refactoring safe as the order of the method parameters is completely irrelevant. + * refactoring-safe as the order of the method parameters is completely irrelevant. * * @param name * @return diff --git a/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java b/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java index 13963517..994cae9f 100644 --- a/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java +++ b/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java @@ -22,6 +22,7 @@ import static org.springframework.data.domain.Sort.Direction.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -88,13 +89,13 @@ public class SimpleUserRepositoryTests { } @Test - public void useGuavaOptionalInsteadOfNulls() { + public void useOptionalAsReturnAndParameterType() { - assertThat(repository.findByUsername("foobar").isPresent(), is(false)); + assertThat(repository.findByUsername(Optional.of("foobar")), is(Optional.empty())); repository.save(user); - assertThat(repository.findByUsername("foobar").isPresent(), is(true)); + assertThat(repository.findByUsername(Optional.of("foobar")).isPresent(), is(true)); } @Test