我该如何解决这个 Spring UnsatisfiedDependencyException
How can I solve this Spring UnsatisfiedDependencyException
我刚开始学习 Spring 和 Hibernate,我正在尝试做一些练习,但遇到了一些问题。基本结构是sample,像这样。
但是,我遇到了 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
我做的代码在这个Link:https://github.com/LehTia/schoolmanagementsystem。我已经看了一整天,但找不到。谁能快速看一下我做了什么并告诉我哪里出了问题?
如有任何帮助,我们将不胜感激。
非常感谢。
问题出在 mappedBy 属性,您设置的 属性 "users" 不正确。在您的情况下,您应该使用:
@OneToMany(fetch=FetchType.LAZY,
mappedBy="institution",
cascade={CascadeType.DETACH, CascadeType.MERGE,CascadeType.PERSIST, CascadeType.REFRESH})
private List<Users> users;
即使您在两个表之间建立关系,也只有其中一个表对另一个表具有外键约束。
"MappedBy allows you to still link from the table not containing the constraint to the other table."
请阅读Can someone please explain mappedBy in hibernate? and http://www.baeldung.com/hibernate-one-to-many
我刚开始学习 Spring 和 Hibernate,我正在尝试做一些练习,但遇到了一些问题。基本结构是sample,像这样。
但是,我遇到了 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
我做的代码在这个Link:https://github.com/LehTia/schoolmanagementsystem。我已经看了一整天,但找不到。谁能快速看一下我做了什么并告诉我哪里出了问题?
如有任何帮助,我们将不胜感激。 非常感谢。
问题出在 mappedBy 属性,您设置的 属性 "users" 不正确。在您的情况下,您应该使用:
@OneToMany(fetch=FetchType.LAZY,
mappedBy="institution",
cascade={CascadeType.DETACH, CascadeType.MERGE,CascadeType.PERSIST, CascadeType.REFRESH})
private List<Users> users;
即使您在两个表之间建立关系,也只有其中一个表对另一个表具有外键约束。 "MappedBy allows you to still link from the table not containing the constraint to the other table." 请阅读Can someone please explain mappedBy in hibernate? and http://www.baeldung.com/hibernate-one-to-many