title: Configuring Hibernate Search with EntityManager
body: <p>I want to add Hibernate Search to my project, I write sample code like this to test it.</p>
  
  <pre><code> EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pu");
   EntityManager em = entityManagerFactory.createEntityManager();
  
   FullTextEntityManager fullTextSession = Search.getFullTextEntityManager(em);
   em.getTransaction().begin();
  
   QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Place.class).get();
  
   double centerLatitude = 0d;
   double centerLongitude = 0d;
   org.apache.lucene.search.Query luceneQuery = builder
                  .spatial()
                  .within(100, Unit.KM)
                  .ofLatitude(centerLatitude)
                  .andLongitude(centerLongitude)
                  .createQuery();
  
   javax.persistence.Query jpaQuery =
                  fullTextSession.createFullTextQuery(luceneQuery, Place.class);
  
    List result = jpaQuery.getResultList();
  
   em.getTransaction().commit();
   em.close();
  </code></pre>
  
  <p>And Im getting exception like this.</p>
  
  <pre><code>javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu
      javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
      javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
      pl.project.repository.place.CustomPlaceRepositoryImpl.findAll(CustomPlaceRepositoryImpl.java:24)
      pl.project.service.place.PlaceServiceImpl.findNearest(PlaceServiceImpl.java:157)
      pl.project.webapp.HomeController.helloFacebook(HomeController.java:41)
  </code></pre>
  
  <p>Here is my Repository config. </p>
  
  <pre><code> @Configuration
   @EnableTransactionManagement
   @EnableAspectJAutoProxy(proxyTargetClass = true)
   @PropertySource("classpath:jdbc.properties")
   @EnableJpaRepositories(value = "pl.project")
   @ComponentScan(basePackages = "pl.project.repository", includeFilters =     @ComponentScan.Filter(value = Repository.class, type =  FilterType.ANNOTATION))
   public class RepositoryConfig {
  
      //...
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactoryBean.setPersistenceUnitName("pu");
          entityManagerFactoryBean.setDataSource(dataSource());
          entityManagerFactoryBean.setPackagesToScan("pl.project.model");
          entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
          entityManagerFactoryBean.setJpaProperties(jpaProperties());
          entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
          return entityManagerFactoryBean;
      }
  
      @Bean
      public Properties jpaProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", hbm);
          properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
          return properties;
      }
  
      @Bean
      public HibernateJpaVendorAdapter jpaVendorAdapter() {
          HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();            hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
          return hibernateJpaVendorAdapter;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
          return transactionManager;
      }
  
      @Bean
      public DriverManagerDataSource dataSource() {
          //... driver, pass, username
          return dataSource;
      }
  }
  </code></pre>
  
  <p>I have created persistence unit in <code>entityManagerFactory()</code> named <code>pu</code>, so why I'm getting exception? This repository configuration has worked before I added Hibernate search.</p>
  
  <p>Do I need add <code>persistence.xml</code>? There is way to configure it by JavaConfig?</p>
  
  <p>My dependecies</p>
  
  <pre><code>    &lt;!-- Hibernate --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;5.0.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;5.0.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-search-orm&lt;/artifactId&gt;
          &lt;version&gt;5.5.3.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>