level 1
lshz0088
楼主
我的理解是这样的:
如果service层的方法没有让spring去管理事务,那么
this.sessionFactory.getCurrentSession() 这个方法是获取不到session的,需要自己手动开启一个session
遇到的问题:
①Service层
public interface UserServiceI {
public User login(User user);
}
②Dao层
public class BaseDaoImpl<T> implements BaseDaoI<T> {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
private Session getCurrentSession() {
//就是这里,取不到session,报错forg.hibernate.HibernateException: No Session found or current thread
return this.sessionFactory.getCurrentSession();
}
}
③事物拦截的配置
<tx:attributes>
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
★拦截的是所有的service实现类
在事物拦截的配置中加入了<tx:method name="login" />就可以使用了。
可是为什么我看视频里的老师,没有加入事务管理,也能取到session?
是不是我哪里理解错了?
2015年04月23日 18点04分
1
如果service层的方法没有让spring去管理事务,那么
this.sessionFactory.getCurrentSession() 这个方法是获取不到session的,需要自己手动开启一个session
遇到的问题:
①Service层
public interface UserServiceI {
public User login(User user);
}
②Dao层
public class BaseDaoImpl<T> implements BaseDaoI<T> {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
private Session getCurrentSession() {
//就是这里,取不到session,报错forg.hibernate.HibernateException: No Session found or current thread
return this.sessionFactory.getCurrentSession();
}
}
③事物拦截的配置
<tx:attributes>
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
★拦截的是所有的service实现类
在事物拦截的配置中加入了<tx:method name="login" />就可以使用了。
可是为什么我看视频里的老师,没有加入事务管理,也能取到session?
是不是我哪里理解错了?