英文:
Spring Boot JPA Query Error - "No property '' found for type '' " Exception
问题
我目前正在学习一个关于使用Spring Data JPA的在线Spring Boot课程。
我的项目包括两个实体:BDProject和BDUser,它们之间有多对一的关系。当尝试根据用户ID查找项目时,会显示以下异常。
异常
org.springframework.beans.factory.UnsatisfiedDependencyException:使用字段‘bdProjectService’表达的不满足的依赖关系创建bean时出错;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:使用字段‘bdProjectRepository’表达的不满足的依赖关系创建bean时出错;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称‘BDProjectRepository’创建bean时出错;调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:为公共抽象方法创建查询失败,方法为java.util.List com.project.bdproject.BDProjectRepository.findByUserID(java.lang.String);在BDProject类型中未找到属性userID!
我花了几个小时来尝试找出是什么导致了这个异常,但似乎没有什么可以修复它的方法。
我的代码:
实体
@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);
}
非常感谢您的所有帮助!谢谢!
英文:
I am currently studying an online Spring Boot course working with Spring Data JPA.
My project includes 2 entities: BDProject and BDUser which have a many to one relationship. When attempting to find projects from user id the following exception is displayed.
EXCEPTION
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!
I have spent hours trying to figure out what's causing this exception, but nothing seems to be fixing it.
MY CODE:
Entities
@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...
Controller
@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
@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);
}
}
Repository
public interface BDProjectRepository extends CrudRepository<BDProject, String>{
public List<BDProject> findByUserID(String userID);
}
Any and all help is much appreciated. Thanks!
答案1
得分: 1
在BDProject中,您有属性
private BDUser bdUser;
而在存储库中,您有:
public List<BDProject> findByUserID(String userID);
错误指出在BDProject中您没有名为userID的属性,这是正确的,因为您有bdUser。
因此,请将
findByUserID(String userID) 更改为 findByBdUserUserID(String userID)。
英文:
In BDProject you have property
private BDUser bdUser;
and in the repository you have:
public List<BDProject> findByUserID(String userID);
Error states that in BDProject you don't have property userID which is correct since you have bdUser.
Therefore, please change
findByUserID(String userID) to findByBdUserUserID(String userID)
答案2
得分: 0
你已经为BDProject实体创建了一个BDProjectRepository接口。
请修改该存储库中的方法:
当前:public List<BDProject> findByUserID(String userID);
修改为:public List<BDProject> findByProID(String proID);
如果你想要获取特定用户的BDProject,可以通过查询相关对象来检索,如下所示:
public List<BDProject> findByBdUser_UserID(String proID);
英文:
You have created a BDProjectRepository interface for BDProject entity.
Please modify the method in that repository:
now: public List<BDProject> findByUserID(String userID);
should be: public List<BDProject> findByProID(String proID);
If you want to get BDProject for a specific user you can retrieve it by querying the related object as
public List<BDProject> findByBdUser_UserID(String proID);
答案3
得分: 0
当在引用对象中按字段查询时,应该像这样编写 ChildObject_ChildID
public interface BDProjectRepository extends CrudRepository<BDProject, String>
{
public List<BDProject> findByBdUser_UserID(String userID);
}
英文:
When querying by fields in referenced object you should write it like ChildObject_ChildID
public interface BDProjectRepository extends CrudRepository<BDProject, String>
{
public List<BDProject> findByBdUser_UserID(String userID);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论