Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'com.example.srilanka.model.Employee'

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> {

}
like image 593
ruwan liyanage Avatar asked Sep 07 '25 07:09

ruwan liyanage


1 Answers

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.

like image 60
YCF_L Avatar answered Sep 10 '25 09:09

YCF_L