Spring 引导 JPA 查询错误 - "No property '' found for type '' " 异常
Spring Boot JPA Query Error - "No property '' found for type '' " Exception
我目前正在学习使用 Spring Data JPA 的在线 Spring 引导课程。
我的项目包括 2 个实体:BDProject 和 BDUser,它们具有多对一关系。尝试从用户 ID 查找项目时,显示以下异常。
异常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'BDProjectController': Unsatisfied dependency expressed through field 'bdProjectService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'BDProjectService': Unsatisfied dependency expressed through field 'bdProjectRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BDProjectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.project.bdproject.BDProjectRepository.findByUserID(java.lang.String)! No property userID found for type BDProject!
我花了几个小时试图找出导致此异常的原因,但似乎没有任何解决办法。
我的代码:
实体
@Entity
public class BDUser {
@Id
private String userID;
private String bdUsername;
private String bdUserEmail;
private String bdUserPassword;
public BDUser(){
}
public BDUser(String userID, String bdUsername, String bdUserEmail, String bdUserPassword) {
super();
this.userID = userID;
this.bdUsername = bdUsername;
this.bdUserEmail = bdUserEmail;
this.bdUserPassword = bdUserPassword;
}
// getters and setters...
@Entity
public class BDProject {
@Id
private String proID;
private String proName;
private String proCodeOwner;
private String proIDs;
@ManyToOne
private BDUser bdUser;
public BDProject() {
}
public BDProject(String proID, String proName, String proCodeOwner, String proIDs, String userID) {
super();
this.proID = proID;
this.proName = proName;
this.proCodeOwner = proCodeOwner;
this.proIDs = proIDs;
this.bdUser = new BDUser(userID, "", "", "");
}
// getters and setters...
控制器
@RestController
public class BDProjectController {
@Autowired
private BDProjectService bdProjectService;
@RequestMapping("/bdusers/{userID}/bdprojects")
public List<BDProject> getAllProjects(@PathVariable String proID){
return bdProjectService.getAllProjects(proID);
}
@RequestMapping("/bdusers/{userID}/bdprojects/{proID}")
public BDProject getProject(@PathVariable String proID){
return bdProjectService.getProject(proID);
}
@RequestMapping(method= RequestMethod.POST, value="/bdusers/{userID}/bdprojects")
public void addProject(@RequestBody BDProject bdProject, @PathVariable String userID){
bdProject.setBdUser(new BDUser(userID, "", "", ""));
bdProjectService.addProject(bdProject);
}
@RequestMapping(method= RequestMethod.PUT, value="/bdusers/{userID}/bdprojects/{proID}")
public void updateProject(@RequestBody BDProject bdProject, @PathVariable String userID, @PathVariable String proID){
bdProject.setBdUser(new BDUser(userID, "", "", ""));
bdProjectService.updateProject(bdProject);
}
@RequestMapping(method= RequestMethod.DELETE, value="/bdusers/{userID}/bdprojects/{proID}")
public void deleteProject(@PathVariable String proID){
bdProjectService.deleteProject(proID);
}
}
服务
@Service
public class BDProjectService {
@Autowired
private BDProjectRepository bdProjectRepository;
public List<BDProject> getAllProjects(String userID){
List<BDProject> bdProjects = new ArrayList<>();
bdProjectRepository.findByUserID(userID).forEach(bdProjects::add);
return bdProjects;
}
public BDProject getProject(String proID){
return bdProjectRepository.findById(proID).orElse(null);
}
public void addProject(BDProject BDProject){
bdProjectRepository.save(BDProject);
}
public void updateProject(BDProject BDProject){
bdProjectRepository.save(BDProject);
}
public void deleteProject(String proID){
bdProjectRepository.deleteById(proID);
}
}
存储库
public interface BDProjectRepository extends CrudRepository<BDProject, String>{
public List<BDProject> findByUserID(String userID);
}
非常感谢任何和所有的帮助。谢谢!
您已经为 BDProject 实体创建了 BDProjectRepository 接口。
请修改该存储库中的方法:
现在:public List<BDProject> findByUserID(String userID);
应该是:public List<BDProject> findByProID(String proID);
如果您想为特定用户获取 BDProject,您可以通过查询相关对象来检索它
public List<BDProject> findByBdUser_UserID(String proID);
当按引用对象中的字段查询时,您应该这样写 ChildObject_ChildID
public interface BDProjectRepository extends CrudRepository<BDProject, String>
{
public List<BDProject> findByBdUser_UserID(String userID);
}
在 BDProject 中你有 属性
private BDUser bdUser;
并且在您的存储库中:
public List<BDProject> findByUserID(String userID);
错误指出在 BDProject 中您没有 属性 userID,这是正确的,因为您有 bdUser。
因此,请更改
findByUserID(String userID) to findByBdUserUserID(String userID)
我目前正在学习使用 Spring Data JPA 的在线 Spring 引导课程。
我的项目包括 2 个实体:BDProject 和 BDUser,它们具有多对一关系。尝试从用户 ID 查找项目时,显示以下异常。
异常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'BDProjectController': Unsatisfied dependency expressed through field 'bdProjectService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'BDProjectService': Unsatisfied dependency expressed through field 'bdProjectRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BDProjectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.project.bdproject.BDProjectRepository.findByUserID(java.lang.String)! No property userID found for type BDProject!
我花了几个小时试图找出导致此异常的原因,但似乎没有任何解决办法。
我的代码:
实体
@Entity
public class BDUser {
@Id
private String userID;
private String bdUsername;
private String bdUserEmail;
private String bdUserPassword;
public BDUser(){
}
public BDUser(String userID, String bdUsername, String bdUserEmail, String bdUserPassword) {
super();
this.userID = userID;
this.bdUsername = bdUsername;
this.bdUserEmail = bdUserEmail;
this.bdUserPassword = bdUserPassword;
}
// getters and setters...
@Entity
public class BDProject {
@Id
private String proID;
private String proName;
private String proCodeOwner;
private String proIDs;
@ManyToOne
private BDUser bdUser;
public BDProject() {
}
public BDProject(String proID, String proName, String proCodeOwner, String proIDs, String userID) {
super();
this.proID = proID;
this.proName = proName;
this.proCodeOwner = proCodeOwner;
this.proIDs = proIDs;
this.bdUser = new BDUser(userID, "", "", "");
}
// getters and setters...
控制器
@RestController
public class BDProjectController {
@Autowired
private BDProjectService bdProjectService;
@RequestMapping("/bdusers/{userID}/bdprojects")
public List<BDProject> getAllProjects(@PathVariable String proID){
return bdProjectService.getAllProjects(proID);
}
@RequestMapping("/bdusers/{userID}/bdprojects/{proID}")
public BDProject getProject(@PathVariable String proID){
return bdProjectService.getProject(proID);
}
@RequestMapping(method= RequestMethod.POST, value="/bdusers/{userID}/bdprojects")
public void addProject(@RequestBody BDProject bdProject, @PathVariable String userID){
bdProject.setBdUser(new BDUser(userID, "", "", ""));
bdProjectService.addProject(bdProject);
}
@RequestMapping(method= RequestMethod.PUT, value="/bdusers/{userID}/bdprojects/{proID}")
public void updateProject(@RequestBody BDProject bdProject, @PathVariable String userID, @PathVariable String proID){
bdProject.setBdUser(new BDUser(userID, "", "", ""));
bdProjectService.updateProject(bdProject);
}
@RequestMapping(method= RequestMethod.DELETE, value="/bdusers/{userID}/bdprojects/{proID}")
public void deleteProject(@PathVariable String proID){
bdProjectService.deleteProject(proID);
}
}
服务
@Service
public class BDProjectService {
@Autowired
private BDProjectRepository bdProjectRepository;
public List<BDProject> getAllProjects(String userID){
List<BDProject> bdProjects = new ArrayList<>();
bdProjectRepository.findByUserID(userID).forEach(bdProjects::add);
return bdProjects;
}
public BDProject getProject(String proID){
return bdProjectRepository.findById(proID).orElse(null);
}
public void addProject(BDProject BDProject){
bdProjectRepository.save(BDProject);
}
public void updateProject(BDProject BDProject){
bdProjectRepository.save(BDProject);
}
public void deleteProject(String proID){
bdProjectRepository.deleteById(proID);
}
}
存储库
public interface BDProjectRepository extends CrudRepository<BDProject, String>{
public List<BDProject> findByUserID(String userID);
}
非常感谢任何和所有的帮助。谢谢!
您已经为 BDProject 实体创建了 BDProjectRepository 接口。 请修改该存储库中的方法:
现在:public List<BDProject> findByUserID(String userID);
应该是:public List<BDProject> findByProID(String proID);
如果您想为特定用户获取 BDProject,您可以通过查询相关对象来检索它
public List<BDProject> findByBdUser_UserID(String proID);
当按引用对象中的字段查询时,您应该这样写 ChildObject_ChildID
public interface BDProjectRepository extends CrudRepository<BDProject, String>
{
public List<BDProject> findByBdUser_UserID(String userID);
}
在 BDProject 中你有 属性
private BDUser bdUser;
并且在您的存储库中:
public List<BDProject> findByUserID(String userID);
错误指出在 BDProject 中您没有 属性 userID,这是正确的,因为您有 bdUser。
因此,请更改
findByUserID(String userID) to findByBdUserUserID(String userID)