在使用sessionFactory.getCurrentSession();时,提示找不到当前session
该问题的产生是由于sessionFatory未能够正确注册。
具体原因如下:
需要配置事务bean,具体如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="ServiceOperation"
expression="execution(* com.cml.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="ServiceOperation" />
</aop:config>
再到 Service 中配置
...
@Override
@Transactional(propagation=Propagation.SUPPORTS, readOnly=false)//加上这一句
public boolean addOrUpdateUser(User user) {
return userDao.addOrUpdateUser(user);
}
...