Spring整合Hibernate事务时报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springfremework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: org.springfremework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springfremework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException: org.springfremework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
........
检查beans.xml是否全都配置正确,事务配置为:
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
最开始找了半天也没发现哪里错,最后发现是beans.xml配置中写错了,很低级的错误。比较好的做法是把常用的配置写好放着,等要用的时候直接可以复制过去,可以避免错误。
此外,在整合Spring和Hibernate时,使用@Transactiona,通常将事务放在service中,因为考虑到可能一个业务逻辑中有多个数据库操作,如果放在DAO的话,当后面的操作失败时,前面的操作不能回滚。