From d748923519ecb0094afec240eadbefce23223671 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 7 Jul 2014 16:08:05 +0200 Subject: [PATCH] Added sample for invocation of JDK 8 default method on a repository. --- jpa/java8/pom.xml | 4 ++++ .../springdata/jpa/java8/CustomerRepository.java | 6 ++++++ .../springdata/jpa/java8/Java8IntegrationTests.java | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/jpa/java8/pom.xml b/jpa/java8/pom.xml index 1e6a5eb7..653c7884 100644 --- a/jpa/java8/pom.xml +++ b/jpa/java8/pom.xml @@ -10,6 +10,10 @@ spring-data-jpa-java8 Spring Data JPA - Java 8 specific features + + + Evans-BUILD-SNAPSHOT + diff --git a/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java b/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java index d4113f42..d1d0c491 100644 --- a/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java +++ b/jpa/java8/src/main/java/example/springdata/jpa/java8/CustomerRepository.java @@ -36,4 +36,10 @@ public interface CustomerRepository extends Repository { Optional findOne(Long id); S save(S customer); + + Optional findByLastname(String lastname); + + default Optional findByLastname(Customer customer) { + return findByLastname(customer.lastname); + } } diff --git a/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java b/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java index 7a8d8f76..4275ab9e 100644 --- a/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java +++ b/jpa/java8/src/test/java/example/springdata/jpa/java8/Java8IntegrationTests.java @@ -56,4 +56,14 @@ public class Java8IntegrationTests { assertThat(customer.createdDate, is(notNullValue())); assertThat(customer.modifiedDate, is(notNullValue())); } + + @Test + public void invokesDefaultMethod() { + + Customer customer = repository.save(new Customer("Dave", "Matthews")); + Optional result = repository.findByLastname(customer); + + assertThat(result.isPresent(), is(true)); + assertThat(result.get(), is(customer)); + } }