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;
|
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();
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue