Spring和hibernate search 整合出现的问题~
准备用hibernate search和lucene做一个搜索模块的,但在整合Spring的时候出现了一点问题:
hibernate search会帮我们自动创建索引文件,如果你在hibernate配置文件中配置了相关属性,当我们对数据库进行修改时,索引文件也跟着更新,但这时必须要用org.hibernate.search.FullTextSession这个类进行操作,而FullTextSession是由session为参数生成的:fullTextSession = Search.createFullTextSession(session).
当我们用Spring管理hibernate时,会让Spring自动管理事务,这时候我们就会继承HibernateDaoSupport类,并且使用HibernateTemplate对数据库进行操作,为了能让索引文件自动更新,我想使用FullTextSession
所用调用了以下方法:
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);
但是getCurrentSession()时就会报错:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
大概意思是session没有绑定到线程上,没有进行事务管理,我在网上也查了,只查到一条信息说需要在配置文件中加上
<prop key="hibernate.current_session_context_class">thread</prop>
但是我试了仍然报错。。。知道的朋友请指点下。。
也可以给另外的思路,怎么让hibernate serach+lucene+spring整合起来。。谢谢了~