I'm getting above mentioned error (Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'com.example.srilanka.model.Employee'
) while engaging with spring boot project. I already referred all articles under this topic where in the stackoverflow as well as another tutorials. But I could not find a solution yet.
package com.example.srilanka.dao;
import com.example.srilanka.model.Employee;
import com.example.srilanka.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class EmployeeDAO {
@Autowired
EmployeeRepository employeeRepository;
/*to save an employee*/
public Employee save(Employee emp){
return employeeRepository.save(emp);
}
/*search all employees*/
public List<Employee> findAll(){
return employeeRepository.findAll();
}
/*update an employee by id*/
public Employee findOne(int empId){
return employeeRepository.findOne(empId); /*<----------error arise in here
}
/*get an employee*/
/*delete an emmployee*/
}
my EmployeeRepository is in here
package com.example.srilanka.repository;
import com.example.srilanka.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
From the documentation findOne return an Optional<S>
public <S extends T> Optional<S> findOne(Example<S> example)
So instead you have two ways .orElse(null)
, to get even the object or null if it is not present :
return employeeRepository.findOne(empId).orElse(null);
else change the type of your method to Optional
public Optional<Employee> findOne(int empId) {
return employeeRepository.findOne(empId);
}
or you can even use orElseThrow
to throw an exception if the object not exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With