我正在使用Spring JAP建立简单的ManyToOne关系。我得到UnsatisfiedDependencyException错误,bean名称通过字段表示不满意的依赖关系
UnsatisfiedDependencyException:创建名称为“ procjectController”的bean时出错:不满意
这是我的档案。
project.java
package com.ganesh.dto;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
public class Project {
@Id
private int projectId;
private String projectName;
//Relation establish
@ManyToOne(
fetch = FetchType.LAZY,
optional = false
)
@JoinColumn(
name = "employee_id",
nullable = false
)
@JsonIgnore
private Employee employee;
public Project() {
}
public Project(int projectId, String projectName, int eId) {
super();
this.projectId = projectId;
this.projectName = projectName;
//Adding employee
this.employee = new Employee(eId,"","");
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
//Adding getter and setters Employee reference
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
ProjectDao.java
package com.ganesh.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.ganesh.dto.Project;
@Repository
public interface ProjectDao extends JpaRepository<Project, Integer> {
List<Project> findEmployeeById(int eId);
}
ImpProjectService.java
package com.ganesh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ganesh.dao.*;
import com.ganesh.dto.Project;
@Service
public class ImpProjectService implements ProjectService {
@Autowired
private ProjectDao projectDao;
@Override
public List<Project> getProjectList(int eId) {
System.out.println("in Dao class employee id"+ eId);
return projectDao.findEmployeeById(eId);
}
@Override
public Project getProjectById(int id) {
return projectDao.getOne(id);
}
@Override
public void addProject(Project project) {
projectDao.save(project);
}
@Override
public void updateProject(Project project) {
projectDao.save(project);
}
@Override
public void deleteProjectById(int id) {
projectDao.deleteById(id);
}
@Override
public List<Project> getAllProject() {
return projectDao.findAll();
}
}
ProcjectController.java
package com.ganesh.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ganesh.dto.*;
import com.ganesh.service.*;
@RestController
public class ProcjectController {
@Autowired
private ImpProjectService projectService;
@RequestMapping("/projects")
public List<Project> getProjectList(){
return projectService.getAllProject();
}
@RequestMapping("/employees/{eId}/projects")
public List<Project> getAllProjects(@PathVariable int eId){
System.out.println("In Project Controller");
List<Project> projList = projectService.getProjectList(eId);
System.out.println(projList);
return projList;
}
@RequestMapping("/employees/{eId}/projects/{id}")
public Project getProjectById(@PathVariable int id) {
return projectService.getProjectById(id);
}
@RequestMapping(method = RequestMethod.POST, value="/employees/{eId}/projects")
public void addProject(@RequestBody Project project, @PathVariable int eId) {
project.setEmployee(new Employee(eId,"",""));
projectService.addProject(project);
}
@RequestMapping(method = RequestMethod.PUT, value="/employees/{eId}/projects/{id}")
public void updateProject(@RequestBody Project project, @PathVariable int eId) {
project.setEmployee(new Employee(eId,"",""));
projectService.updateProject(project);
}
@RequestMapping(method = RequestMethod.DELETE, value="/projects/{id}")
public void deleteProjecstById(@PathVariable int id) {
projectService.deleteProjectById(id);
}
}
最佳答案
注意:此答案基于数据不足,因为堆栈跟踪不可用。使用正确且完整的堆栈跟踪,我们也许可以提供更精确的答案。
回答:
在您的Dao课堂上看起来像是一个问题。
你写
@Repository
public interface ProjectDao extends JpaRepository<Project, Integer> {
List<Project> findEmployeeById(int eId);
}
这意味着您正在创建类型为
Project
的存储库,并尝试将查询作为findEmployeeById
触发。它应该是findByEmployee
(接受Employee
作为参数),或者根本不存在。因为查询语法和Template参数不匹配。因此,Spring将无法为其初始化查询处理程序。如果可以满足您的目的,请尝试如下进行更改。
@资料库
公共接口ProjectDao扩展了JpaRepository {
List<Project> findAllByEmployee(Employee emp);
}
请检查相同,然后更正。如果仍然无法使用,请发布完整的堆栈跟踪,我们会为您提供帮助。