求高手帮忙解决当使用getCurrentSession 获取session时 ssh框架中的事务配置问题!
本人一新人,现在公司是我IT行业的第一家公司。
我在学习java的再做ssh项目的时候,讲师讲的案例是把事务配置到dao中,例如代码:
<aop:config>
<aop:pointcut expression="execution (* org.netctoss.cost.dao..*.*(..))"
id="aopCut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="aopCut" />
</aop:config>
在现在的公司中也是发现了这样的问题,也是把事务配置到dao中。
但是我在网上看到的说法是配置到service中,例如我配置的:
<aop:config>
<aop:pointcut expression="execution (* test.service..*.*(..))" id="curd"/>
<aop:advisor advice-ref="idAdvice" pointcut-ref="curd"/>
</aop:config>
但是我发现登陆时就报错,错误点是我在到中使用的session获取的方法Session session = sessionFactory.getCurrentSession();
我在网上查到要使用Session session = sessionFactory.getCurrentSession();这个方法就需要配置事务,如果不配置事务getCurrentSession();
它是取不到session的。
问题就出来了,getCurrentSession()在dao层, 配置事务在dao层就能用这个方法,但是我想配置事务到service层也能用这个方法。这要怎么解决????
有高手帮忙解决么??
我现在想到的方法就是使用HibernateTemplate 代替 session ,然后把事务配置到service。 例如,如下代码:
private SessionFactory sessionFactory;
HibernateTemplate ht;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
ht = new HibernateTemplate(sessionFactory);
}
我在dao层取得HibernateTemplate 用它来完成session 的功能,但是我感觉HibernateTemplate 不如session 好用,
还有别的方法能让我即把事务配置到service 有能用 getCurrentSession()么?求高手!!!!!!!!