#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.
This commit is contained in:
@@ -13,16 +13,9 @@
|
||||
|
||||
<name>Spring Data JPA - Example</name>
|
||||
<description>Small sample project showing the usage of Spring Data JPA.</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>16.0.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Hopper-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -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, Long> {
|
||||
*/
|
||||
User findByTheUsersName(String username);
|
||||
|
||||
Optional<User> findByUsername(String username);
|
||||
/**
|
||||
* Uses {@link Optional} as return and parameter type.
|
||||
*
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
Optional<User> findByUsername(Optional<String> 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<User, Long> {
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user