我配置好了但是运行就产生错误,比如一个User类和角色(role)类,它们之间是多对一的关系,我在User里家Role类型的属性,可以为空。现在向User表保存数据,单独用hibernate没问题,但是用到Spring就出现 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'userDao' while setting bean property 'userDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.scs.model.Roleps:我在配置文件里配置了com.scs.model.Role
nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.scs.model.Role 这个异常信息最有价值,没看你代码,检查下Role.hbm.xml文件中为Role指定的主键生成策略是否遗漏
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'userDao' while setting bean property 'userDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.scs.model.Roleps:我在配置文件里配置了com.scs.model.Role
这个异常信息最有价值,没看你代码,检查下Role.hbm.xml文件中为Role指定的主键生成策略是否遗漏
这个类没配主键,主键用<id>,不用<property>