Merge pull request #4313 from krnkhanna1989/BAEL-1467

changes for optional and serializable.
This commit is contained in:
Tom Hombergs 2018-05-22 21:42:24 +02:00 committed by GitHub
commit 6674850c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 12 additions and 9 deletions

View File

@ -1,12 +1,14 @@
package com.baeldung.spring.data.keyvalue.services; package com.baeldung.spring.data.keyvalue.services;
import java.util.Optional;
import com.baeldung.spring.data.keyvalue.vo.Employee; import com.baeldung.spring.data.keyvalue.vo.Employee;
public interface EmployeeService { public interface EmployeeService {
void save(Employee employee); void save(Employee employee);
Employee get(Integer id); Optional<Employee> get(Integer id);
Iterable<Employee> fetchAll(); Iterable<Employee> fetchAll();

View File

@ -27,9 +27,8 @@ public class EmployeeServicesWithKeyValueTemplate implements EmployeeService {
} }
@Override @Override
public Employee get(Integer id) { public Optional<Employee> get(Integer id) {
Optional<Employee> employee = keyValueTemplate.findById(id, Employee.class); return keyValueTemplate.findById(id, Employee.class);
return employee.isPresent() ? employee.get() : null;
} }
@Override @Override

View File

@ -1,5 +1,7 @@
package com.baeldung.spring.data.keyvalue.services.impl; package com.baeldung.spring.data.keyvalue.services.impl;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -26,8 +28,8 @@ public class EmployeeServicesWithRepository implements EmployeeService {
} }
@Override @Override
public Employee get(Integer id) { public Optional<Employee> get(Integer id) {
return employeeRepository.findById(id).get(); return employeeRepository.findById(id);
} }
@Override @Override

View File

@ -6,7 +6,7 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.keyvalue.annotation.KeySpace; import org.springframework.data.keyvalue.annotation.KeySpace;
@KeySpace("employees") @KeySpace("employees")
public class Employee implements Serializable { public class Employee {
@Id @Id
private Integer id; private Integer id;

View File

@ -50,7 +50,7 @@ public class EmployeeServicesWithKeyValueRepositoryIntegrationTest {
@Test @Test
public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() {
Employee employeeFetched = employeeService.get(1); Employee employeeFetched = employeeService.get(1).get();
assertEquals(employeeFetched, employee1); assertEquals(employeeFetched, employee1);
} }

View File

@ -46,7 +46,7 @@ public class EmployeeServicesWithRepositoryIntegrationTest {
@Test @Test
public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() {
Employee employeeFetched = employeeService.get(1); Employee employeeFetched = employeeService.get(1).get();
assertEquals(employeeFetched, employee1); assertEquals(employeeFetched, employee1);
} }