在service中新增了一个检验用户名、密码的方法,由于是拼的SQL语句,
最后调用的时候,是这么写的:
Query countQuery = this.baseDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(countHql.append(whereHql).toString());
但是一直调试的时候,一直报:
“org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here”
改成:
Query countQuery = this.baseDAO.getHibernateTemplate().getSessionFactory().openSession().createSQLQuery(countHql.append(whereHql).toString());
就没问题,但是这样写的,session用完了之后,不会自动关闭。所以还是打算找出不能用getCurrentSession的原因。
发现在 公用的applicationContext.xml文件中没有配置“事务监听”。
下面介绍一下配置事务监听的两种方法:
1、根据后缀判断。(如action/service)
2、根据开头去判断。(如find/delete等)
我这里采用的是 第二种方法,配完了之后,重启服务,还是报那个错,后来仔细查找发现,还要修改各自的applicationContext.xml。
(采用方法2时,必须修改每一个Service的配置,都改成用事务代理,用方法一则不需要修改。)
这样重启服务,再运行this.baseDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(countHql.append(whereHql).toString());
就可以通过了。