Issue on adding a custom functionality to a Spring Data Repository

Subha Bhowmik :

I'm getting data through inbuilt methods of CrudRepository, but getting error while applying customized method to data repository.

This is the repository

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
    List<Employee> findAllByEmployeeLastName(String last);
}

This is the Service Class

@Service
public class EmployeeService {

    private EmployeeRepository employeeRepository;

    @Autowired
    public EmployeeService(EmployeeRepository _employeeRepository) {
        employeeRepository = _employeeRepository;
    }

    public Iterable<Employee> findAll() {
        return employeeRepository.findAll();
    }

    public Employee findById(int id) {

        Optional<Employee> id_of_employee = employeeRepository.findById(id);

        Employee employee = null;

        if (id_of_employee.isPresent()) {
            employee = id_of_employee.get();
        } else {
            throw new RuntimeException("ID not found : " + id);
        }

        return employee;
    }

    public List<Employee> getEmployeeByLastName(String last) {
        return employeeRepository.findAllByEmployeeLastName(last);
    }
}

This is the Controller

@RestController
@RequestMapping("/api") public class EmployeeRestController {

    public EmployeeService employeeService;

    @Autowired
    public EmployeeRestController(EmployeeService _employeeService) {
        employeeService = _employeeService;
    }

    @GetMapping("/list_of_employees")
    public Iterable<Employee> getAllEmployees() {
        return employeeService.findAll();
    }

    @GetMapping("/find_an_employee_by_id/{id}")
    public Employee getAnEmplopyeeByID(@PathVariable int id) {
        Employee employee = employeeService.findById(id);

        if (employee == null) {
            throw new RuntimeException("ID not found : " + id);
        }

        return employee;

    }

    @RequestMapping("/find_employee_by_last_name/{last}")
    public List<Employee> getEmployeeByLastName(@PathVariable(value = "last") String last) {
        return employeeService.getEmployeeByLastName(last);
    }

}

getEmployeeByLastName() method is the customized method here, it's giving error, do I have to add some xml file to run a custom method? Or am I going wrong somewhere else? Would really appreciate any assist on this.

This is the entity class

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "first_name")
    private String first_name;

    @Column(name = "last")
    private String last;

    @Column(name = "salary")
    private double salary;

    @Column(name = "email")
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last= last;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Employee() {
    }
}

Stack Trace:

[I:\spring_boot_project_factory\project_factory_1\section_1\Project\target\classes\com\project\service\EmployeeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.project.repository.EmployeeRepository.findByEmployeeLastName(java.lang.String)! No property employeeLastName found for type Employee! 2019-04-19 15:02:25.321 INFO 3920 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2019-04-19 15:02:25.334 INFO 3920 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2019-04-19 15:02:25.345 INFO 3920 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2019-04-19 15:02:25.348 INFO 3920 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-04-19 15:02:25.390 INFO 3920 --- [ restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-04-19 15:02:25.409 ERROR 3920 --- [ restartedMain] o.s.boot.SpringApplication
: Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeRestController' defined in file

Adil Khalil :

In the logs, it is stated that

Failed to create query for method public abstract java.util.List com.project.repository.EmployeeRepository.findByEmployeeLastName(java.lang.String)! No property employeeLastName found for type Employee

your method name in EmployeeRepository should be findAllByLast(String last);

Also i would recommend to please read about java coding and naming conventions so you can avoid such mistakes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149856&siteId=1