views:

116

answers:

0

I'm trying to integrate Hibernate Search with Spring, but I can't seem to index anything. I was able to get Hibernate Search to work without Spring, but I'm having a problem integrating it with Spring. Any help would be much appreciated.

Below is my springmvc-servlet.xml:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="enewsclipsPersistenceUnit" />
</bean>

And here is my DAO class:

@Repository
public class SearchDaoImpl implements SearchDao {
 JpaTemplate jpaTemplate; 

 @Autowired
 public SearchDaoImpl(EntityManagerFactory entityManagerFactory) {
  this.jpaTemplate = new JpaTemplate(entityManagerFactory); 
 }

 @SuppressWarnings("unchecked")
 public void updateSearchIndex() {

  /* Implement the callback method */
  jpaTemplate.execute(new JpaCallback() {
   public Object doInJpa(EntityManager em) throws PersistenceException {
    List<Article> articles = em.createQuery("select a from Article a").getResultList();
    FullTextEntityManager ftEm = Search.getFullTextEntityManager(em);
    ftEm.getTransaction().begin();
    for(Article article : articles) {
     System.out.println("Indexing Item " + article.getTitle());
     ftEm.index(article);
    }
    ftEm.getTransaction().commit();
    return null;
   }

  }); 
 }
}

I think that it may have to do with the transactions but I'm not exactly sure. If you could just point me in the right direction, that would be helpful too!

Thank you.