Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Failed to create query for method

Error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jenkinsService': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jenkinsRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.crud.JenkinsRepo.findByrun_id(java.lang.String)! No property run found for type Jenkins!

This is the Jenkins class to define table entity:

package com.example.crud;

import javax.persistence.*;

@Entity
@Table(name="test_case_failure")
public class Jenkins {

    @Id
    @Column(name = "failure_id")
    private int failure_id;

    @Column(name="test_case_name")
    private String test_case_name;
    @Column(name="expected_value")
    private String expected_value;
    @Column(name="error_name")
    private String error_name;
    @Column(name="auto_error_type")
    private String auto_error_type;
    @Column(name="run_id")
    private String run_id;

    public Jenkins() {
    }

    public int getFailure_id() {
        return failure_id;
    }

    public void setFailure_id(int failure_id) {
        this.failure_id = failure_id;
    }

    public String getTest_case_name() {
        return test_case_name;
    }

    public void setTest_case_name(String test_case_name) {
        this.test_case_name = test_case_name;
    }

    public String getExpected_value() {
        return expected_value;
    }

    public void setExpected_value(String expected_value) {
        this.expected_value = expected_value;
    }

    public String getError_name() {
        return error_name;
    }

    public void setError_name(String error_name) {
        this.error_name = error_name;
    }

    public String getAuto_error_type() {
        return auto_error_type;
    }

    public void setAuto_error_type(String auto_error_type) {
        this.auto_error_type = auto_error_type;
    }

    public String getRun_id() {
        return run_id;
    }

    public void setRun_id(String run_id) {
        this.run_id = run_id;
    }

}

Controller class:

package com.example.crud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class appController {

    @Autowired
    private jenkinsService service;

    @RequestMapping("/")
    public String viewHomePage(Model model) {
        List<Jenkins> listProducts = service.getbyrun_id();
        model.addAttribute("TestsReports", listProducts);

        return "index";
    }

}

Repository class:

package com.example.crud;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface JenkinsRepo extends JpaRepository<Jenkins, Integer> {
    List<Jenkins> findByrun_id(String run_id);
}

Service class:

package com.example.crud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class jenkinsService {

    @Autowired
    private JenkinsRepo repo;

    List<Jenkins> getbyrun_id() {
        return repo.findByrun_id("test");
    }

}
like image 386
Koushik J Avatar asked Nov 02 '25 03:11

Koushik J


1 Answers

spring-data uses the underscore as a separator for nested fields when it tries to inject a query from the method signature. So, if you do findByrun_id Spring will search for the nested field Jenkins.run.id. You should change the attribute run_id to runId and then rename your method to findByrunId or findByRunId

like image 144
robertobatts Avatar answered Nov 03 '25 19:11

robertobatts