Merge pull request #4313 from krnkhanna1989/BAEL-1467
changes for optional and serializable.
This commit is contained in:
commit
6674850c03
|
@ -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<Employee> get(Integer id);
|
||||
|
||||
Iterable<Employee> fetchAll();
|
||||
|
||||
|
|
|
@ -27,9 +27,8 @@ public class EmployeeServicesWithKeyValueTemplate implements EmployeeService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Employee get(Integer id) {
|
||||
Optional<Employee> employee = keyValueTemplate.findById(id, Employee.class);
|
||||
return employee.isPresent() ? employee.get() : null;
|
||||
public Optional<Employee> get(Integer id) {
|
||||
return keyValueTemplate.findById(id, Employee.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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<Employee> get(Integer id) {
|
||||
return employeeRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue