-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Search and Spring
PostPosted: Fri Apr 23, 2010 11:49 am 
Newbie

Joined: Fri Apr 23, 2010 11:43 am
Posts: 2
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:

Code:
<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="pUnitName" />
</bean>


And here is my DAO class:
Code:
@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.


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Fri Apr 23, 2010 4:22 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
I would recommend looking at

http://community.jboss.org/wiki/SpringH ... nateSearch


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Fri Apr 23, 2010 4:47 pm 
Newbie

Joined: Fri Apr 23, 2010 11:43 am
Posts: 2
I had previously gone through all the steps in the link above, but it still doesn't work. It's so strange that it works when I run the code without Spring in a standalone class. I even tried something very simple like this:

Code:
@Repository
public class SearchDaoImpl implements SearchDao {

   public void updateSearchIndex() {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("pUnitName");   
      try {
         EntityManager em = emf.createEntityManager();
         FullTextEntityManager ftEm = Search.getFullTextEntityManager(em);
         /*  Create index */
         ftEm.getTransaction().begin();
         @SuppressWarnings("unchecked")
         List<Article> articles = em.createQuery("select i from Article i").getResultList();
         for(Article article : articles) {
            System.out.println("Indexing Item " + article.getTitle());
            ftEm.index(article);
         }
         ftEm.getTransaction().commit();
      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         emf.close();               
      }            
   }
}


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Sat Apr 24, 2010 1:54 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Can you post a sample entity with the annotations you are using? Also there is a maven project attached to this jira which uses Spring and Hibernate http://opensource.atlassian.com/project ... SEARCH-320. Maybe you could download it and take a look and compare with what you have. It doesn;t use JPA but you should be able to work out the difference.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.