diff --git a/jdbc-template/src/main/java/guru/springframework/jdbctemplate/employee/EmployeeRepository.java b/jdbc-template/src/main/java/guru/springframework/jdbctemplate/employee/EmployeeRepository.java index 7de48a7..1fa20dc 100644 --- a/jdbc-template/src/main/java/guru/springframework/jdbctemplate/employee/EmployeeRepository.java +++ b/jdbc-template/src/main/java/guru/springframework/jdbctemplate/employee/EmployeeRepository.java @@ -76,7 +76,7 @@ public void save(Employee employee) { * @param employee the employee to be created * @return the id of the created employee. */ - public long saveAndReturnId(Employee employee) { + public Long saveAndReturnId(Employee employee) { String sqlQuery = "insert into employees(first_name, last_name, yearly_income) " + "values (?, ?, ?)"; @@ -90,7 +90,11 @@ public long saveAndReturnId(Employee employee) { return stmt; }, keyHolder); - return keyHolder.getKey().longValue(); + if (keyHolder.getKey() != null) { + return keyHolder.getKey().longValue(); + }else { + return null; + } } diff --git a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Controller/ProductController.java b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Controller/ProductController.java index 326d9a1..9108b81 100644 --- a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Controller/ProductController.java +++ b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Controller/ProductController.java @@ -33,7 +33,7 @@ public ResponseEntity> getAllProducts(){ @GetMapping("product/{id}") public ResponseEntity getProductById(@PathVariable("id") int id){ - return new ResponseEntity<>(productService.getProductByid(id),HttpStatus.OK); + return new ResponseEntity<>(productService.getProductById(id),HttpStatus.OK); } @DeleteMapping("product/{id}") diff --git a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductService.java b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductService.java index 7544002..d1185c1 100644 --- a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductService.java +++ b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductService.java @@ -9,7 +9,7 @@ public interface ProductService { Product addProduct(Product product) throws ProductAlreadyExistsException; List getAllProducts(); - Product getProductByid(int id); + Product getProductById(int id); Product deleteProductById(int id); diff --git a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductServiceImpl.java b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductServiceImpl.java index 090c1c6..a657e5a 100644 --- a/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductServiceImpl.java +++ b/testingspringbootrestfulservice/src/main/java/com/springframeworkguru/Service/ProductServiceImpl.java @@ -35,7 +35,7 @@ public List getAllProducts() { } @Override - public Product getProductByid(int id) { + public Product getProductById(int id) { return productRepository.findById(id).orElse(null); }