在使用spring3和hibernate4.2集成与hibernate3有很多的不同,其中之一就是spring3不在支持HibernateTemplate,而是使用hibernate原生的api,我在集成的时候遇到了如下两个问题。
问题之一:在使用session.save()方法保存数据时不能成功的保存到数据库
这个问题的原因是在获取session时,不能使用openSession()方法,而要使用getCurrentSession()方法
- @Resource(name="sf")
- private SessionFactory sessionFactory;
- Session session ;
- ...
- session = sessionFactory.getCurrentSession();
问题之二:使用getCurrentSession时报:hibernate4 org.hibernate.HibernateException: No Session的错误
这个问题的原因是session未打开,解决方式:
前提记得在service层上面加上@Transaction注释,否则任然会有相同的异常
方式1-在web.xml中加过滤器
- <!-- open session filter -->
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
方式2-配置事务的切面
- <!-- 用spring管理事务 -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sf"/>
- </bean>
- <!-- 配置使用注解的方式来使用事务 -->
- <tx:annotation-driven transaction-manager="transactionManager" />
- <!-- 配置那些类的方法进行事务管理,需要aopalliance-1.0.jar和aspectjweaver.jar,当前com.neusoft.leehom.service包中的子包,
- 类中所有方法需要,还需要参考tx:advice的设置 -->
- <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="insert*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- <!-- 需要引入aop的命名空间 -->
- <aop:config>
- <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
- <aop:pointcut id="daoMethods" expression="execution(* com.tl..serviceimpl.*.*(..))" />
- <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
- <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
- </aop:config>
在使用Spring mvc + Spring + Hibernate4配置的时候,总是出现 No Session found for current thread,仔细检查applicationContext.xml和dispacter-servlet.xml文件,注解、事务配置都没有问题,纠结好久。
网上搜了很多方法,都不能解决。
有说加上<prop key="hibernate.current_session_context_class">thread</prop>的配置。有说hibernate4要加上的是<propkey="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>,经过测试,都不能解决。
看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,觉得说的有道理,还是事务没有装配。
试着把dispacter-servlet.xml中的内容合并到applicationContext.xml中,采用一个配置文件(当然web.xml也要做相应修改),发现并没有报错,问题解决。
但是自己还是喜欢使用两个配置文件,这样结构更清晰。
分析为什么合并到一起就没问题呢,原来spring的context是父子容器,所以会产生冲突,Controller会首先被扫描装配,而此时的Service还没有进行事务的配置,获得的将是原样的Service(没有经过事务装配,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描设备进行事务处理。
这样就好办了,让两个配置文件各干各的事就可以了。
1 、在Spring主容器中(applicationContext.xml),用<context:exclude-filter/>将Controller的注解过滤掉,不扫描装配它。
- <context:component-scan base-package="com">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
2 而在springMVC配置文件中(dispatcher-servlet.xml)将Service和Dao的注解给过滤掉 ,只扫描装配Controller。
- <context:component-scan base-package="com">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
- </context:component-scan>