如何解决这个Spring UnsatisfiedDependencyException

时间:2018-06-04 13:03:11

标签: hibernate spring-mvc dependency-injection hibernate-mapping

我刚刚开始学习Spring和Hibernate而且我正在尝试做一些练习,但我遇到了一些问题。基本结构就是样本,就像这样。 enter image description here

但是,我遇到了UnsatisfiedDependencyException:

的问题
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'administratorController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userDAO'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-mvc-school-management-sys-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.demo.schoolmanagementsystem.entity.Users.users in com.demo.schoolmanagementsystem.entity.Institution.users

我所做的代码在此链接中:https://github.com/LehTia/schoolmanagementsystem。我整天都在看,但我找不到它。谁能快速看看我做了什么,告诉我哪里出错了?

任何帮助将不胜感激。 很多。

1 个答案:

答案 0 :(得分:1)

问题在于mappedBy属性,您设置的属性“users”不正确。在您的情况下,您应该使用:

@OneToMany(fetch=FetchType.LAZY,
            mappedBy="institution",
            cascade={CascadeType.DETACH, CascadeType.MERGE,CascadeType.PERSIST, CascadeType.REFRESH})
private List<Users> users;

即使你在两个表之间建立关系,只有其中一个表对另一个表有外键约束。 “MappedBy允许您仍然从不包含约束的表链接到另一个表。” 请阅读Can someone please explain mappedBy in hibernate?http://www.baeldung.com/hibernate-one-to-many