[英]issue while using spring data gemfire repository functionality while querying data from region
[英]Issue on adding a custom functionality to a Spring Data Repository
我通過CrudRepository
的內置方法獲取數據,但在將自定義方法應用於數據存儲庫時出錯。
這是存儲庫
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
List<Employee> findAllByEmployeeLastName(String last);
}
這是服務類
@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);
}
}
這是控制器
@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()
方法是這里的自定義方法,它給出了錯誤,我是否必須添加一些 xml 文件才能運行自定義方法? 還是我在其他地方出錯了? 真的很感激這方面的任何幫助。
這是實體類
@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() {
}
}
堆棧跟蹤:
[I:\spring_boot_project_factory\project_factory_1\section_1\Project\target\classes\com\project\service\EmployeeService.class]:通過構造函數參數0表示的未滿足的依賴; 嵌套異常是 org.springframework.beans.factory.BeanCreationException:創建名為“employeeRepository”的 bean 時出錯:調用 init 方法失敗; 嵌套異常是 java.lang.IllegalArgumentException:無法為方法 public abstract java.util.List com.project.repository.EmployeeRepository.findByEmployeeLastName(java.lang.String) 創建查詢:找不到 Employee 類型的屬性 employeeLastName:2019-04 -19 15.02.25:321 INFO 3920 --- [restartedMain] j:LocalContainerEntityManagerFactoryBean: 關閉 JPA EntityManagerFactory 持久化單元'默認' 2019-04-19 15.02.25.334 INFO 3920 --- [restartedMain] com.zaxxer:hikari。光數據源。 HikariPool-1 - 關閉啟動。:: 2019-04-19 15.02.25.345 INFO 3920 --- [restartedMain] com.zaxxer:hikari.HikariDataSource: HikariPool-1 - 關閉完成:2019-04-19 15.02.25.348 INFO 3920 --- [restartedMain] o.apache.catalina:core:StandardService: 停止服務 [Tomcat] 2019-04-19 15.02:25.390 INFO 3920 --- [restartedMain] ConditionEvaluationReportLoggingListener :
啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序。 2019-04-19 15:02:25.409 錯誤 3920 --- [restartedMain] osboot.SpringApplication
: 應用程序運行失敗org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為“employeeRestController”的 bean 在文件中定義時出錯
在日志中,指出
無法為方法 public abstract java.util.List com.project.repository.EmployeeRepository.findByEmployeeLastName(java.lang.String) 創建查詢! 找不到 Employee 類型的屬性 employeeLastName
您在EmployeeRepository
中的方法名稱應該是findAllByLast(String last);
此外,我建議您閱讀有關 Java 編碼和命名約定的信息,這樣您就可以避免此類錯誤。
在數據存儲庫中創建自定義方法時,您的方法命名約定是強制性的,取決於您的 DAO 類字段。
在您的姓氏 DAO 類中,您創建了last
變量,因此您的方法名稱不能是 findAllByLastName(String last),因為您的 DAO 類中沒有任何名為 lastName 的變量。
所以應該是findAllByLast(String last);
或findByLast(String last)
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.