Hibernate4.x与spring3.x整合,有关事务的处理,用Junit4测试,出现org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/SessionFactoryImplementor;
xml主要配置信息:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>com.li.bean.User</value> <value>com.li.bean.Log</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/>
解决办法:并不是缺少jar包。目前spring3.x已经支持hibernate4.x。所以有些以前的hibernate3字眼改成hibernate4即可!正如
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">与
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">改成
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
不出现报错了!