Reputation: 37
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!
Upvotes: 1
Views: 742
Reputation: 126
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);
}
Upvotes: 0
Reputation: 2575
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)
Upvotes: 1
Reputation: 591
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);
Upvotes: 0