diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java index dd89609be7..83117b3baa 100644 --- a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/EmployeeService.java @@ -1,12 +1,14 @@ package com.baeldung.spring.data.keyvalue.services; +import java.util.Optional; + import com.baeldung.spring.data.keyvalue.vo.Employee; public interface EmployeeService { void save(Employee employee); - Employee get(Integer id); + Optional get(Integer id); Iterable fetchAll(); diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java index 26f1756add..3eb1d0f66a 100644 --- a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithKeyValueTemplate.java @@ -27,9 +27,8 @@ public class EmployeeServicesWithKeyValueTemplate implements EmployeeService { } @Override - public Employee get(Integer id) { - Optional employee = keyValueTemplate.findById(id, Employee.class); - return employee.isPresent() ? employee.get() : null; + public Optional get(Integer id) { + return keyValueTemplate.findById(id, Employee.class); } @Override diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java index 73f3493a6b..70e00dadc0 100644 --- a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/services/impl/EmployeeServicesWithRepository.java @@ -1,5 +1,7 @@ package com.baeldung.spring.data.keyvalue.services.impl; +import java.util.Optional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -26,8 +28,8 @@ public class EmployeeServicesWithRepository implements EmployeeService { } @Override - public Employee get(Integer id) { - return employeeRepository.findById(id).get(); + public Optional get(Integer id) { + return employeeRepository.findById(id); } @Override diff --git a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java index 208e6e1735..1063c578f3 100644 --- a/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java +++ b/spring-data-keyvalue/src/main/java/com/baeldung/spring/data/keyvalue/vo/Employee.java @@ -6,7 +6,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.keyvalue.annotation.KeySpace; @KeySpace("employees") -public class Employee implements Serializable { +public class Employee { @Id private Integer id; diff --git a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueRepositoryIntegrationTest.java b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueRepositoryIntegrationTest.java index e376f1b804..37b0a69a2d 100644 --- a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueRepositoryIntegrationTest.java +++ b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithKeyValueRepositoryIntegrationTest.java @@ -50,7 +50,7 @@ public class EmployeeServicesWithKeyValueRepositoryIntegrationTest { @Test public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { - Employee employeeFetched = employeeService.get(1); + Employee employeeFetched = employeeService.get(1).get(); assertEquals(employeeFetched, employee1); } diff --git a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryIntegrationTest.java b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryIntegrationTest.java index bb034d6aee..d0ed1506f8 100644 --- a/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryIntegrationTest.java +++ b/spring-data-keyvalue/src/test/java/com/baeldung/spring/data/keyvalue/services/test/EmployeeServicesWithRepositoryIntegrationTest.java @@ -46,7 +46,7 @@ public class EmployeeServicesWithRepositoryIntegrationTest { @Test public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { - Employee employeeFetched = employeeService.get(1); + Employee employeeFetched = employeeService.get(1).get(); assertEquals(employeeFetched, employee1); }