0

I want to add Hibernate Search to my project, I write sample code like this to test it.

 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();

And Im getting exception like this.

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)

Here is my Repository config.

 @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;
    }
}

I have created persistence unit in entityManagerFactory() named pu, so why I'm getting exception? This repository configuration has worked before I added Hibernate search.

Do I need add persistence.xml? There is way to configure it by JavaConfig?

My dependecies

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.5.3.Final</version>
    </dependency>
Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58

1 Answers1

3

You set up the persistence unit via Spring's LocalContainerEntityManagerFactoryBean but are then using plain JPA (Persistence) in order to obtain it.

So you either need to obtain the entity manager (factory) through dependency injection using Spring, or you set up persistence.xml so you can obtain it via plain JPA.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
  • You have right, my mistake was result of ignorance. I used `@PersistenceContext EnitityManager em;` instead of trying to create new persistence unit. After few months of using JPA / Hibernate I'm still feel unconfident. – Jakub Pomykała Jun 13 '16 at 16:18