package com.exadel.search; import org.hibernate.search.jpa.FullTextEntityManager; import org.hibernate.search.jpa.Search; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * The only meaning for this class is to build the Lucene index at application * startup. This is needed in this example because the database is filled * before and each time the web application is started. In a normal web * application probably you don't need to do this. * * @author netgloo */ @Component public class BuildSearchIndex implements ApplicationListener<ContextRefreshedEvent> { @PersistenceContext private EntityManager entityManager; @Override public void onApplicationEvent(final ContextRefreshedEvent event) { try { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { System.out.println( "An error occurred trying to build the serach index: " + e.toString()); } return; } }