使用spring4.3.7整合hibernate4.2.21应注意以下几点:
1. 使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean装配SessionFactory,
会报异常: java.lang.NoClassDefFoundError:[Lorg/hibernate/engine/FilterDefinition;
解决办法:
使用org.springframework.orm.hibernate4.LocalSessionFactoryBean装配SessionFactory,即:
<beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
2. 使用org.springframework.orm.hibernate3.HibernateTransactionManager装配TransactionManager
会报异常:java.lang.NoClassDefFoundError:org/hibernate/context/CurrentSessionContext
解决办法:
使用org.springframework.orm.hibernate4.HibernateTransactionManager装配TransactionManager,即
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
同时修改:
<propkey="hibernate.current_session_context_class">thread</prop>
为:
<propkey="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
3. 使用getHibernateTemplate()方法
会报异常:java.lang.ClassCastException:org.springframework.orm.hibernate4.SessionHolder cannot be cast toorg.springframework.orm.hibernate3.SessionHolder
解决办法:使用hibernate自带方法getCurrentSession()创建Session,通过自身方法完成数据库操作